我使用以下代码从C#调用PS函数:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = PowerShellExe,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true,
WorkingDirectory = workingDirectory,
Arguments = string.Concat(string.Format(@" -File ""{0}"" ", fullPathToFile), commandLineArguments)
},
EnableRaisingEvents = true
};
在我执行某些正则表达式的函数中,通过替换配置文件的某些设置。结果如下:
<rabbitMQ messageBusURI="rabbitmq://localhost/" messageBusUserName="guest" messageBusPassword="guest" purgeOnStartup="true" timeout="00:00:30" />
唯一的问题是我无法逃避双引号,因此以
结尾<rabbitMQ messageBusURI=rabbitmq://localhost messageBusUserName=guest messageBusPassword=guest purgeOnStartup=false timeout=00:00:30 />
执行正则表达式的PS是
param(
[string]$FilePath = $Args[0],
[string]$RegEx = $Args[1],
[string]$ReplacementValue = $Args[2]
)
cls
(get-content $FilePath) | foreach-object {$_ -replace $RegEx,$ReplacementValue} | set-content $FilePath
用于调用它的commandLineArguments的值是:
" -FilePath \"MyService.exe.config\" -RegEx \"<rabbitMQ messageBusURI=[^\\>]*>\" -ReplacementValue \"<rabbitMQ messageBusURI=\"rabbitmq://localhost\" messageBusUserName=\"guest\" messageBusPassword=\"guest\" purgeOnStartup=\"false\" timeout=\"00:00:30\" />\" "
如何在commandLineArguments中转义双引号,以便将它们放在最终的配置文件中?