C# Run long CMD process and capture output to WinForm

Mr Cheese 1 Reputation point
2021-04-19T03:50:26.997+00:00

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.

Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,651 Reputation points
    2021-04-20T08:55:17.297+00:00

    Hi MrCheese-8474,
    Please try the following code:

    private void Form1_Load(object sender, EventArgs e)  
    {  
        System.Diagnostics.Process process = new System.Diagnostics.Process();  
        process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;  
        process.StartInfo.FileName = "cmd.exe";  
        process.StartInfo.Arguments = @"/C DIR C:\";  
        process.StartInfo.UseShellExecute = false;  
        process.StartInfo.CreateNoWindow = true;  
        process.StartInfo.RedirectStandardOutput = true;  
        process.StartInfo.RedirectStandardInput = true;  
        process.Start();  
        string q = "";  
        while (!process.HasExited)  
        {  
            q += process.StandardOutput.ReadToEnd();  
        }  
        textBox1.Text = q;  
    //    MessageBox.Show(q);  
    }  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

  2. Viorel 122.6K Reputation points
    2021-04-19T10:37:04.187+00:00

    Try removing RedirectStandardInput=true and replace it with RedirectStandardOutput=true, RedirectStandardError=true.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.