正在将C++控制台输出重定向到C#

本文关键字:重定向 输出 控制台 C++ | 更新日期: 2023-09-27 18:22:22

我正在尝试将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);

谢谢,

正在将C++控制台输出重定向到C#

使用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();

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