question

MrCheese-8474 avatar image
0 Votes"
MrCheese-8474 asked DanielZhang-MSFT answered

C# Run long CMD process and capture output to WinForm

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.

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.