How to capture the standard output/error of a Process?

13,805

Solution 1

By redirecting it and reading the stream.

Solution 2

To solve the deadlock problems use this approach:

ProcessStartInfo hanging on "WaitForExit"? Why?

Works well in my code...

Share:
13,805
Louis Rhys
Author by

Louis Rhys

trying to learn and help others learn :)

Updated on June 04, 2022

Comments

  • Louis Rhys
    Louis Rhys almost 2 years

    How does one capture the standard output/error of a process started by a Process.Start() to a string?

  • Kiquenet
    Kiquenet over 9 years
    You need to set UseShellExecute to true for the Verb to be respected and it must be set to 'false' to redirect standard output. You can't do both. I'm pretty sure Windows also won't allow you to redirect standard input/output/error across the admin/non-admin security boundary. You'll have to find a different way to get output from the program running as admin - Reference: stackoverflow.com/a/8690661
  • tstone2077
    tstone2077 over 9 years
    -1. As stated by the MSDN Documentation: "A deadlock condition results if the parent process calls p.StandardOutput.ReadToEnd followed by p.StandardError.ReadToEnd and the child process writes enough text to fill its error stream". Your code does exactly that.
  • tstone2077
    tstone2077 over 9 years
    -1: The linked article runs into a deadlock issue (at least, at the time of writing this): As stated by the MSDN Documentation: "A deadlock condition results if the parent process calls p.StandardOutput.ReadToEnd followed by p.StandardError.ReadToEnd and the child process writes enough text to fill its error stream".