How to spawn a process and capture its STDOUT in .NET?


I need to spawn a child process that is a console application, and capture its output.

I wrote up the following code for a method:

string retMessage = String.Empty;
ProcessStartInfo startInfo = new ProcessStartInfo();
Process p = new Process();

startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;

startInfo.UseShellExecute = false;
startInfo.Arguments = command;
startInfo.FileName = exec;

p.StartInfo = startInfo;

p.OutputDataReceived += new DataReceivedEventHandler
    delegate(object sender, DataReceivedEventArgs e)
        using (StreamReader output = p.StandardOutput)
            retMessage = output.ReadToEnd();


return retMessage;

However, this does not return anything. I don't believe the OutputDataReceived event is being called back, or the WaitForExit() command may be blocking the thread so it will never callback.

Any advice?

EDIT: Looks like I was trying too hard with the callback. Doing:

return p.StandardOutput.ReadToEnd(); 

Appears to work fine.

5/13/2015 11:40:52 PM

Accepted Answer

This can be quite easily achieved using the ProcessStartInfo.RedirectStandardOutput property. A full sample is contained in the linked MSDN documentation; the only caveat is that you may have to redirect the standard error stream as well to see all output of your application.

Process compiler = new Process();
compiler.StartInfo.FileName = "csc.exe";
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;


8/12/2011 1:14:53 PM

This is bit improvement over accepted answer from @mdb. Specifically, we also capture error output of the process. Additionally, we capture these outputs through events because ReadToEnd() doesn't work if you want to capture both error and regular output. It took me while to make this work because it actually also requires BeginxxxReadLine() calls after Start().

using System.Diagnostics;

Process process = new Process();

void LaunchProcess()
    process.EnableRaisingEvents = true;
    process.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(process_OutputDataReceived);
    process.ErrorDataReceived += new System.Diagnostics.DataReceivedEventHandler(process_ErrorDataReceived);
    process.Exited += new System.EventHandler(process_Exited);

    process.StartInfo.FileName = "some.exe";
    process.StartInfo.Arguments = "param1 param2"
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.RedirectStandardOutput = true;


    //below line is optional if we want a blocking call

void process_Exited(object sender, EventArgs e)
    Console.WriteLine(string.Format("process exited with code {0}\n", process.ExitCode.ToString()));

void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    Console.WriteLine(e.Data + "\n");

void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    Console.WriteLine(e.Data + "\n");

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow