我试图理解为什么当我调用上述函数时,我正在读取的输出中每第 80 列都会收到十六进制 0D0A。
我有一个用于测试的 powershell 脚本,为简洁起见,其中有两行:
$xmlSpew = "<IISAppPoolConfig><DefaultWebSite><ApplicationPoolName>DefaultAppPool</ApplicationPoolName></DefaultWebSite><AuthN>Basic</AuthN></IISAppPoolConfig>"
Write-Output $xmlSpew
我使用 Process 对象和 ProcessStartInfo 调用脚本,如下所示:
var psi = new ProcessStartInfo
{
WorkingDirectory = Path.GetDirectoryName(FileToRun),
FileName = FileToRun,
Arguments = Arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
};
var process = new Process
{
StartInfo = psi,
EnableRaisingEvents = true,
};
FileToRun 值为:
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe
参数值为:
-File "C:\Program Files\MyTest\MyProgInputs\read.d\IISAppPoolConfig.ps1"
脚本运行良好,我返回退出代码 0,但我在标准输出中每 80 个字符就有一个神秘的(对我来说)0D0A 换行符,我使用以下方法捕获:
var Stdout = new List<string>;
...
Stdout.Add(process.StandardOutput.ReadToEnd());
一旦我将标准输出存储在字符串变量中,这就会对我的 XML 工作造成严重破坏。我希望得到在 ps1 脚本中写入 stdout 的准确内容,而不是额外的换行符。
我错过了什么?我已经寻找过其他人解决这个问题,但我还没有找到答案。希望搜索挑战的不是我。
最佳答案
关注this P/Invoke 方法并将 dwXCountChars 设置为一个非常大的值。不要忘记在标志中包含 STARTF_USECOUNTCHARS
。
关于c# - Process.StandardOutput.ReadToEnd() 和无关的换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11295922/