Redirecting C++ console output to C#

六月ゝ 毕业季﹏ 提交于 2019-12-08 13:01:55

问题


I'm trying to get the output of the C++ console to the C# Windows Form application, the problem I'm having is the output of the C++ exe is displayed in the C# console only after the C++ exe is terminated. Is there anyway to get the exe output to C# console in real time while running the C++ exe (as in without having to terminate the exe)? Here is how I tried,

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);

Thanks,


回答1:


Use OutputDataReceived event:

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



回答2:


See Console.SetIn() (and SetOut) and Process.StandardOutput



来源:https://stackoverflow.com/questions/9139374/redirecting-c-console-output-to-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!