I am wanting to run a CMD process and instead of a Console Window have all the output sent to a TextBox on the Winform calling it.
I have worked through several ways to do this, but not having any luck in getting a solution to work.
I have posted some code below, which I have found documented similar on many posts, but I keep getting stuck with this error:
System.InvalidOperationException: 'StandardOut has not been redirected or the process hasn't started yet.'
On the line using: BeginOutputReadLine();
My Test Code:
private void CMD_Process()
{
Process p = new Process();
p.StartInfo = new ProcessStartInfo("CMD.EXE")
{
RedirectStandardInput = true,
UseShellExecute = false,
Arguments = @"/C DIR C:\"
};
p.OutputDataReceived += p_OutputDataReceived;
p.ErrorDataReceived += p_ErrorDataReceived;
p.Start();
p.BeginOutputReadLine();
}
private void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
this.BeginInvoke(new Action(() => textBoxProgress.AppendText(e.Data)));
}
private void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
this.BeginInvoke(new Action(() => textBoxProgress.AppendText(e.Data)));
}
I am wanting to keep this solution as simple as possible.
My aim to to run a CMD process and have all output displayed live on the WinForm while it is running.
Thanks for any help on this.