How to get the output of a System.Diagnostics.Process? How to get the output of a System.Diagnostics.Process? asp.net asp.net

How to get the output of a System.Diagnostics.Process?


What you need to do is capture the Standard Output stream:

p.StartInfo.RedirectStandardOutput = true;p.StartInfo.UseShellExecute = false;// instead of p.WaitForExit(), dostring q = "";while ( ! p.HasExited ) {    q += p.StandardOutput.ReadToEnd();}

You may also need to do something similar with StandardError. You can then do what you wish with q.

It is a bit finicky, as I discovered in one of my questions

As Jon Skeet has pointed out, it is not smart performance-wise to use string concatenation like this; you should instead use a StringBuilder:

p.StartInfo.RedirectStandardOutput = true;p.StartInfo.UseShellExecute = false;// instead of p.WaitForExit(), doStringBuilder q = new StringBuilder();while ( ! p.HasExited ) {    q.Append(p.StandardOutput.ReadToEnd());}string r = q.ToString();


Lucas' answer has a race condition: If the process finishes quickly the while loop is left (or never entered) even if there is some output left, that is you might miss out on some data. To prevent that, another ReadToEnd should be done after the process exited.

(Note that in comparison to the old version of my answer, I can no longer see a need for WaitForExit once the process.HasExited flag is true, so this boils down to:)

using (var process = Process.Start(startInfo)){    var standardOutput = new StringBuilder();    // read chunk-wise while process is running.    while (!process.HasExited)    {        standardOutput.Append(process.StandardOutput.ReadToEnd());    }    // make sure not to miss out on any remaindings.    standardOutput.Append(process.StandardOutput.ReadToEnd());    // ...}


For a more specific answer directly related to ffmpeg, passing the "-report" command into ffmpeg will make it dump a log into the current directory with what was said in the display of the process.

‘-report’

Dump full command line and console output to a file named program-YYYYMMDD-HHMMSS.log in the current directory. This file can be useful for bug reports. It also implies -loglevel verbose.

Note: setting the environment variable FFREPORT to any value has the same effect.

From FFMpeg Documentation.