At the moment I am starting a batch file from my C# program with:
System.Diagnostics.Process.Start(@"DoSomeStuff.bat");
What I would like to be able to do is redirect the output (stdout and stderr) of that child process to the Output window in Visual Studio (specifically Visual C# Express 2008).
Is there a way to do that?
(Additionally: such that it's not all buffered up and then spat out to the Output window when the child process finishes.)
(BTW: At the moment I can get stdout (but not stderr) of the parent process to appear in the Output window, by making my program a "Windows Application" instead of a "Console Application". This breaks if the program is run outside Visual Studio, but this is ok in my particular case.)
Same idea for
Error
, just replaceOutput
in those method/property names.Have you considered using a DefaultTraceListener ?
A variation of this works for me --posting this now because I wish I'd found it earlier. Note that this is just a fragment extracted from the real code so there may be trivial errors.
The technique is based on some MSDN code. What I haven't been able to figure out is how to get the output window to update "on the fly". It doesn't update until after this task returns.
What's going on here is that Visual Studio is displaying the debug output from the program in the Output Window. That is: if you use Trace.WriteLine, it'll appear in the Output Window, because of the default trace listener.
Somehow, your Windows Form application (when it uses Console.WriteLine; I'm assuming you're using Console.WriteLine) is also writing debug output, and Visual Studio is picking this up.
It won't do the same for child processes, unless you explicitly capture the output and redirect it along with your output.