Lots of answers to other questions tell you that you can capture the output of a process using code looking something like this:
public async Task Run()
{
await Task.Run(() =>
{
using (Process process = new Process()
{
StartInfo = new ProcessStartInfo
{
WindowStyle = ProcessWindowStyle.Normal,
RedirectStandardOutput = true,
RedirectStandardError = true,
FileName = "cmd",
Arguments = "path/filename.bat",
}
})
{
process.OutputDataReceived += Process_OutputDataReceived;
process.ErrorDataReceived += Process_ErrorDataReceived;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
});
}
private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
log.Error(e.Data);
}
}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
log.Info(e.Data);
}
}
There are lots of variants which achieve the same thing more or less but they all redirect the standard output/errors - is there a way to capture them while still having them show in the cmd window?