Export (0) Print
Expand All

Process.StandardError Property

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

[Visual Basic]
Public ReadOnly Property StandardError As StreamReader
[C#]
public StreamReader StandardError {get;}
[C++]
public: __property StreamReader* get_StandardError();
[JScript]
public function get StandardError() : StreamReader;

Property Value

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

Exceptions

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

Remarks

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

Note    UseShellExecute on the StartInfo property must be false if you want to set StandardError 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.RedirectStandardError = true;
p.StartInfo.FileName = "test.exe";
p.Start();
p.WaitForExit();
string output = p.StandardError.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.RedirectStandardError = true;
p.StartInfo.FileName = "test.exe";
p.Start();
string output = p.StandardError.ReadToEnd();
p.WaitForExit();

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();
p.WaitForExit();

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.

Example

[Visual Basic, C#, C++] The following example uses the net use command together with a user supplied argument to map a network resource. It then reads the standard error of net.exe and writes it on to console.

[Visual Basic] 
Dim myProcess As New Process()
Dim myProcessStartInfo As New ProcessStartInfo("net ", "use " + args(1))

myProcessStartInfo.UseShellExecute = False
myProcessStartInfo.RedirectStandardError = True
myProcess.StartInfo = myProcessStartInfo
myProcess.Start()

Dim myStreamReader As StreamReader = myProcess.StandardError
' Read the standard error of net.exe and write it on to console.
Console.WriteLine(myStreamReader.ReadLine())
myProcess.Close()

[C#] 
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("net ","use "+ args[0]);

myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardError = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();

StreamReader myStreamReader = myProcess.StandardError;
// Read the standard error of net.exe and write it on to console.
Console.WriteLine( myStreamReader.ReadLine());
myProcess.Close();

[C++] 
Process* myProcess = new Process();
ProcessStartInfo* myProcessStartInfo = new ProcessStartInfo(S"net ", String::Concat(S"use ", args[0]));

myProcessStartInfo->UseShellExecute = false;
myProcessStartInfo->RedirectStandardError = true;
myProcess->StartInfo = myProcessStartInfo;
myProcess->Start();

StreamReader* myStreamReader = myProcess->StandardError;
// Read the standard error of net.exe and write it on to console.
Console::WriteLine(myStreamReader->ReadLine());
myProcess->Close();

[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.

Requirements

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 | StandardOutput | ProcessStartInfo.RedirectStandardError

Show:
© 2014 Microsoft