0

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,

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Anshu
  • 275
  • 3
  • 20

2 Answers2

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();
max
  • 33,369
  • 7
  • 73
  • 84
0

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433