c# - Process.StandardOutput.ReadToEnd() 和无关的换行符

标签 c# windows .net-4.0 process

我试图理解为什么当我调用上述函数时,我正在读取的输出中每第 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/

相关文章:

c++ - 将颜色从RGB转换为NV12

cin 和 CTRL + Z 的 C++ 问题

winforms - 如何在非客户区插入新按钮?

C# 调试包含 lambda 表达式的函数

c# - 如何按字母顺序对字母数字列表进行排序

c# - 覆盖 cookie 时出现问题

windows - celery WindowsError : [Error 6] The handle is invalid

c# - 使用 Async 和 Await 进行异步编程

vb.net - Timer_tick 无法停止的问题

c# - 一个类的某些实例而不是其他实例的静态/共享变量