Process.StandardError プロパティ
アセンブリ: System (system.dll 内)

Public ReadOnly Property StandardError As StreamReader
public StreamReader StandardError { get; }
アプリケーションの標準エラー ストリームの読み取りに使用できる StreamReader。


Process がテキストを標準エラー ストリームに書き込むと、通常、そのテキストはコンソールに表示されます。StandardError ストリームをリダイレクトすることにより、プロセスのエラー出力を操作したり、非表示にしたりできます。たとえば、テキストのフィルタ処理、異なる書式設定の適用、またはコンソールおよび指定したログ ファイルの両方への出力の書き込みを実行できます。
![]() |
---|
StandardError を使用するには、ProcessStartInfo.UseShellExecute を false に設定し、ProcessStartInfo.RedirectStandardError を true に設定する必要があります。設定しない場合は、StandardError ストリームに書き込みを行うと例外がスローされます。 |
リダイレクトされた StandardError ストリームの読み取りは、同期または非同期で実行できます。Read、ReadLine、および ReadToEnd のようなメソッドは、プロセスのエラー出力ストリームで同期読み取り操作を実行します。これらの同期読み取り操作は、関連する Process がその StandardError ストリームに書き込むか、ストリームを閉じるまで完了しません。
対照的に、BeginErrorReadLine は StandardError ストリームで非同期読み取り操作を開始します。このメソッドは、ストリーム出力に指定されたイベント ハンドラを有効にした後、直ちに呼び出し元に制御を返します。これにより、呼び出し元は、ストリーム出力がイベント ハンドラにリダイレクトされている間に他の作業を実行できます。
同期読み取り操作により、StandardError ストリームから読み取る呼び出し元と、そのストリームに書き込む子プロセスの間に依存関係が発生します。この依存関係によってデッドロック状態が発生する場合があります。呼び出し元が子プロセスのリダイレクトされたストリームから読み取る場合、呼び出し元は子に依存します。子がストリームに書き込むまで、またはストリームが閉じるまで、呼び出し元は読み取り操作を待機します。子プロセスから書き込まれるデータによってリダイレクトされたストリームが満杯になる場合、子プロセスは親に依存します。子プロセスは、満杯になったストリームから親がデータを読み取るまで、またはストリームを閉じるまで、次の書き込み操作を待機します。デッドロック状態では、呼び出し元および子プロセスは互いに操作が完了するまで待機するので、どちらも操作を続行できません。デッドロックを回避するには、呼び出し元と子プロセスとの依存関係を検査します。
リダイレクトされたストリームから読み取り、子プロセスが終了するまで待機する方法を、次の C# コード例に示します。
// Start the child process. Process p = new Process(); // Redirect the error stream of the child process. p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardError = true; p.StartInfo.FileName = "Write500Lines.exe"; p.Start(); // Do not wait for the child process to exit before // reading to the end of its redirected error stream. // p.WaitForExit(); // Read the error stream first and then wait. string error = p.StandardError.ReadToEnd(); p.WaitForExit();
このコード例では、p.WaitForExit の前に p.StandardError.ReadToEnd を呼び出してデッドロックを回避しています。親プロセスが p.StandardError.ReadToEnd の前に p.WaitForExit を呼び出し、リダイレクトされたストリームが満杯になるまで子プロセスがテキストを書き込む場合、デッドロック状態が発生する可能性があります。親プロセスは、子プロセスが終了するまで無期限に待機することになります。子プロセスは、満杯になった StandardError ストリームから親がデータを読み取るまで、無期限に待機することになります。
標準出力および標準エラー ストリームの両方からすべてのテキストを読み取る場合にも類似の問題が発生します。両方のストリームで読み取り操作を実行する C# コード例を次に示します。
// Do not perform a synchronous read to the end of both // redirected streams. // string output = p.StandardOutput.ReadToEnd(); // string error = p.StandardError.ReadToEnd(); // p.WaitForExit(); // Use asynchronous read operations on at least one of the streams. p.BeginOutputReadLine(); string error = p.StandardError.ReadToEnd(); p.WaitForExit();
このコード例では、StandardOutput ストリームで非同期読み取り操作を実行してデッドロック状態を回避しています。親プロセスが p.StandardOutput.ReadToEnd の後に p.StandardError.ReadToEnd を呼び出し、エラー ストリームが満杯になるまで子プロセスがテキストを書き込む場合、デッドロック状態が発生する可能性があります。親プロセスは、子プロセスが StandardOutput ストリームを閉じるまで無期限に待機することになります。子プロセスは、満杯になった StandardError ストリームから親がデータを読み取るまで、無期限に待機することになります。
非同期読み取り操作を使用すると、これらの依存関係およびデッドロックの可能性を回避できます。デッドロック状態を回避するには、2 つのスレッドを作成して個別のスレッドで各ストリームの出力を読み取る方法もあります。
![]() |
---|
リダイレクトされたストリームで、非同期読み取り操作と同期読み取り操作を混在させることはできません。Process のリダイレクトされたストリームを非同期モードまたは同期モードで開いた後は、それ以降、そのストリームに対するすべての読み取り操作は同じモードで行う必要があります。たとえば、StandardError で BeginErrorReadLine の後に ReadLine の呼び出しを続けないでください。逆も同様です。ただし、2 つの異なるストリームをそれぞれ別のモードで読み取ることはできます。たとえば、BeginOutputReadLine を呼び出した後で、StandardError ストリーム用に ReadLine を呼び出すことができます。 |

ユーザーにより渡された引数を net use コマンドで使用して、ネットワーク リソースをマップする例を次に示します。この例では、net コマンドの標準エラー ストリームを読み取ってコンソールに書き込みます。
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()
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();
Process^ myProcess = gcnew Process; ProcessStartInfo^ myProcessStartInfo = gcnew ProcessStartInfo( "net ",String::Concat( "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();


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Process クラス
Process メンバ
System.Diagnostics 名前空間
StandardInput
StandardOutput
ProcessStartInfo.RedirectStandardError
Weblioに収録されているすべての辞書からProcess.StandardError プロパティを検索する場合は、下記のリンクをクリックしてください。

- Process.StandardError プロパティのページへのリンク