Summary:
I have a WinForm application doing some works and writing some output with System.Console, Here is the code:
static int Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Console.WriteLine("Some String");
return 1;
}
Suppose we name it SubApp.exe
In another program, I want to execute this subApp.exe and read the output which it creates with System.Diagnostics.Process. Here is my code:
System.Diagnostics.Process p = System.Diagnostics.Process.Start("SubApp.exe", "Some Parameter");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
int exitCode = p.ExitCode;
Question:
But unfortunately, both output and error is empty and even worse the exitCode is 0. What is the problem? Am I doing the procedure in the right way?
System.Diagnosis. And just off the top of my head, double check you are actually pointing at the correct .exe (perhaps accidentally pointed at Debug instead of Release or vice-versa).System.Diagnostics.Process p = System.Diagnosis.Process.Start("SubApp.exe", "Some Parameter");you should useSystem.Diagnostics.Process p = System.Diagnostics.Process.Start("SubApp.exe", "Some Parameter");you have usedDiagnosisDiagnostics