This documentation is archived and is not being maintained.

Process.StandardOutput Property

Gets a stream used to read the application's output.

[Visual Basic]
Public ReadOnly Property StandardOutput As StreamReader
public StreamReader StandardOutput {get;}
public: __property StreamReader* get_StandardOutput();
public function get StandardOutput() : StreamReader;

Property Value

A StreamReader that can be used to read the application's standard output stream.


Exception Type Condition
InvalidOperationException The StandardOutput value has not been defined. The StartInfo property's RedirectStandardOutput might be false.


In order to use StandardOutput, you must have specified true for the StartInfo property's RedirectStandardOutput property. Otherwise, reading the StandardOutput property throws an exception.

Note    UseShellExecute on the StartInfo property must be false if you want to set StandardOutput to true.

The Process component communicates with a child process using a pipe. If a child process writes enough data to the pipe to fill the buffer, the child will block until the parent reads the data from the pipe. This can cause deadlock if your application is reading all output to standard error and standard output. The following C# code, for example, could be problematic.

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "test.exe";
string output = p.StandardOutput.ReadToEnd();

In this instance, both the parent and the child processes would be blocked, as the filled pipe prevents the child process from completing, while the parent process is waiting indefinitely for the child process to exit.

This problem can be solved by moving the ReadToEnd() before the WaitForExit(), as follows.

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "test.exe";
string output = p.StandardOutput.ReadToEnd();

A similar problem arises if you redirect both standard output and standard error and then try to read both, for example using the following C# code.

string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();

In this case, if the child process writes any text to standard error it will block the process, because the parent process cannot read from standard error until it has finished reading from standard output. However, the parent process will not read from standard output until the process ends. A recommended solution to this situation is to create two threads so that your application can read the output of each stream on a separate thread.


[Visual Basic, C#, C++] The following example spawns a new user-defined executable and reads its standard output. The output is then displayed in the console.

[Visual Basic] 
Dim myProcess As New Process()
Dim myProcessStartInfo As New ProcessStartInfo("Process_StandardOutput_Sample.exe")
myProcessStartInfo.UseShellExecute = False
myProcessStartInfo.RedirectStandardOutput = True
myProcess.StartInfo = myProcessStartInfo

Dim myStreamReader As StreamReader = myProcess.StandardOutput
' Read the standard output of the spawned process.
Dim myString As String = myStreamReader.ReadLine()

Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("Process_StandardOutput_Sample.exe" );
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;

StreamReader myStreamReader = myProcess.StandardOutput;
// Read the standard output of the spawned process.
string myString = myStreamReader.ReadLine();

Process* myProcess = new Process();
ProcessStartInfo* myProcessStartInfo = new ProcessStartInfo(S"Process_StandardOutput_Sample.exe");
myProcessStartInfo->UseShellExecute = false;
myProcessStartInfo->RedirectStandardOutput = true;
myProcess->StartInfo = myProcessStartInfo;

StreamReader* myStreamReader = myProcess->StandardOutput;
// Read the standard output of the spawned process.
String* myString = myStreamReader->ReadLine();

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

.NET Framework Security: 

See Also

Process Class | Process Members | System.Diagnostics Namespace | StandardInput | StandardError | ProcessStartInfo.RedirectStandardOutput