I have read the many questions on here about capturing the output from a command in C#, but they all use the StartInfo.UseShellExecute = false
property to redirect output. But this doesn't work for me because I still want the console window to open an display the output there as well.
This allows me to capture the output to a file, but doesn't display the console.
using System.Diagnostics;
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = "/C ping 8.8.8.8 -t";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.Start();
cmd.BeginOutputReadLine();
.
.
.
This displays the console, but doesn't allow me to capture the output.
using System.Diagnostics;
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.Arguments = "/C ping 8.8.8.8 -t";
cmd.Start();
.
.
.
My goal is to launch multiple command windows, each running a given command. However, I also want the console output saved to a log file. One log file per command windows. It is trivial to start the command windows and let them run using their own STDOUT, OR redirect the output to a file. But how can I do both?
I have even gone as far as to try to redirect the output and launch my own WinForms that look like consoles to display the output, but was stopped because each was in a separate process and could not write to the GUI thread.