Process.RedirectStandardOutput 无效

Process.RedirectStandardOutput does not work

我在重定向应用程序的标准输出时遇到问题。这似乎是 .NET 中的某种错误。

我是 运行 Live555ProxyServer,但即使启动的控制台确实有书面输出,我也没有得到任何输出。此代码适用于任何其他控制台应用程序,但不适用于此应用程序。

void StartProcess()
{
    var process = new Process();
    process.StartInfo.FileName = @"live555ProxyServer.exe";
    process.StartInfo.Arguments = "-R";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.OutputDataReceived += process_OutputDataReceived;

    process.Start();
    process.BeginOutputReadLine();
}

void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Debug.WriteLine(e.Data);
}

可以找到该应用程序的源代码here

那是因为所有的输出都转到stderr而不是stdout,见source code

您应该为 Process.ErrorDataReceived and call Process.BeginErrorReadLine 添加一个处理程序,然后事情就会开始顺利进行。