How do I run processes synchronously, targeting the same output?
I have a .Net application that needs to run several executables. I'm using the Process class, but Process.Start doesn't block. I need the first process to finish before the second runs. How can I do this?
Also, I'd like all of the processes to all output to the same console window. As it is, they seem to open their own windows. I'm sure I can use the StandardOutput stream to write to the console, but how can I suppress the defa开发者_JS百科ult output?
I believe you're looking for:
Process p = Process.Start("myapp.exe");
p.WaitForExit();
For output:
StreamReader stdOut = p.StandardOutput;
Then you use it like any stream reader.
To suppress the window it's a bit harder:
ProcessStartInfo pi = new ProcessStartInfo("myapp.exe");
pi.CreateNoWindow = true;
pi.UseShellExecute = true;
// Also, for the std in/out you have to start it this way too to override:
pi.RedirectStandardOutput = true; // Will enable .StandardOutput
Process p = Process.Start(pi);
精彩评论