0

我正在尝试将 C++ 控制台的输出传递给 C# Windows 窗体应用程序,我遇到的问题是 C++ exe 的输出仅在 C++ exe 终止后才会显示在 C# 控制台中。无论如何在运行 C++ exe 时将 exe 输出实时获取到 C# 控制台(如无需终止 exe)?这是我尝试的方法,

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "C:\\path\\ABC.exe";
p.Start();
string output = p.StandardOutput.ReadToEnd();
Console.WriteLine(output);

谢谢,

4

2 回答 2

1

使用OutputDataReceived事件:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "C:\\path\\ABC.exe";
p.OutputDataReceived += (s, e) => Console.WriteLine(e.Data);
p.Start();
p.BeginOutputReadLine();
于 2012-02-04T08:09:05.960 回答
0

请参阅Console.SetIn()(和 SetOut)和Process.StandardOutput

于 2012-02-04T08:04:19.930 回答