Process.StandardOutput プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Process.StandardOutput プロパティの意味・解説 

Process.StandardOutput プロパティ

アプリケーションの出力の読み取りに使用されるストリームを取得します。

名前空間: System.Diagnostics
アセンブリ: System (system.dll 内)
構文構文

Dim instance As Process
Dim value As StreamReader

value = instance.StandardOutput
public StreamReader StandardOutput { get; }
public:
property StreamReader^ StandardOutput {
    StreamReader^ get ();
}
/** @property */
public StreamReader get_StandardOutput ()

プロパティ値
アプリケーションの標準出力ストリームの読み取りに使用できる StreamReader。

例外例外
例外の種類条件

InvalidOperationException

StandardOutput ストリームはリダイレクト用に定義されていません。ProcessStartInfo.RedirectStandardOutput が true に設定され、ProcessStartInfo.UseShellExecute が false に設定されていることを確認してください。

または

StandardOutput ストリームは、BeginOutputReadLine による非同期読み取り操作のために開かれています。

解説解説

Process がテキストを標準ストリームに書き込むと、通常、そのテキストはコンソールに表示されます。StandardOutput ストリームをリダイレクトすることにより、プロセスの出力を操作したり、非表示にしたりできます。たとえば、テキストのフィルタ処理、異なる書式設定の適用、またはコンソールおよび指定したログ ファイルの両方への出力の書き込みを実行できます。

メモメモ

StandardOutput を使用するには、ProcessStartInfo.UseShellExecute を false に設定し、ProcessStartInfo.RedirectStandardOutput を true に設定する必要があります。設定しない場合は、StandardOutput ストリームに書き込みを行うと例外がスローされます。

リダイレクトされた StandardOutput ストリームの読み取りは、同期または非同期で実行できます。Read、ReadLine、および ReadToEnd のようなメソッドは、プロセスの出力ストリームで同期読み取り操作を実行します。これらの同期読み取り操作は、関連する Process がその StandardOutput ストリームに書き込むか、ストリームを閉じるまで完了しません。

対照的に、BeginOutputReadLine は StandardOutput ストリームで非同期読み取り操作を開始します。このメソッドは、ストリーム出力に指定されたイベント ハンドラを有効にした後、直ちに呼び出し元に制御を返します。これにより、呼び出し元は、ストリーム出力がイベント ハンドラにリダイレクトされている間に他の作業を実行できます。

同期読み取り操作により、StandardOutput ストリームから読み取る呼び出し元と、そのストリームに書き込む子プロセスの間に依存関係が発生します。この依存関係によってデッドロック状態が発生する場合があります。呼び出し元が子プロセスのリダイレクトされたストリームから読み取る場合、呼び出し元は子に依存します。子がストリームに書き込むまで、またはストリームが閉じるまで、呼び出し元は読み取り操作を待機します。子プロセスから書き込まれるデータによってリダイレクトされたストリームが満杯になる場合、子プロセスは親に依存します。子プロセスは、満杯になったストリームから親がデータを読み取るまで、またはストリームを閉じるまで、次の書き込み操作を待機します。デッドロック状態では、呼び出し元および子プロセスは互いに操作が完了するまで待機するので、どちらも操作を続行できません。デッドロックを回避するには、呼び出し元と子プロセスとの依存関係を検査します。

リダイレクトされたストリームから読み取り、子プロセスが終了するまで待機する方法を、次の C# コード例に示します。

 // Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = 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 stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();

このコード例では、p.WaitForExit の前に p.StandardOutput.ReadToEnd を呼び出してデッドロックを回避しています。親プロセスが p.StandardOutput.ReadToEnd の前に p.WaitForExit を呼び出し、リダイレクトされたストリームが満杯になるまで子プロセスがテキストを書き込む場合、デッドロック状態が発生する可能性があります。親プロセスは、子プロセスが終了するまで無期限に待機することになります。子プロセスは、満杯になった StandardOutput ストリームから親がデータを読み取るまで、無期限に待機することになります。

標準出力および標準エラー ストリームの両方からすべてのテキストを読み取る場合にも類似の問題が発生します。両方のストリームで読み取り操作を実行する 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 のリダイレクトされたストリームを非同期モードまたは同期モードで開いた後は、それ以降、そのストリームに対するすべての読み取り操作は同じモードで行う必要があります。たとえば、StandardOutput で BeginOutputReadLine の後に ReadLine の呼び出しを続けないでください。逆も同様です。ただし、2 つの異なるストリームをそれぞれ別のモードで読み取ることはできます。たとえば、BeginOutputReadLine を呼び出した後で、StandardError ストリーム用に ReadLine を呼び出すことができます。

使用例使用例

ユーザー定義の新しい実行可能ファイルを作成し、その標準出力を読み取る例を次に示します。読み取った出力は、コンソールに表示します。

Dim myProcess As New Process()
Dim myProcessStartInfo As New
 ProcessStartInfo("Process_StandardOutput_Sample.exe")
myProcessStartInfo.UseShellExecute = False
myProcessStartInfo.RedirectStandardOutput = True
myProcess.StartInfo = myProcessStartInfo
myProcess.Start()

Dim myStreamReader As StreamReader = myProcess.StandardOutput
' Read the standard output of the spawned process.
Dim myString As String =
 myStreamReader.ReadLine()
Console.WriteLine(myString)
myProcess.Close()
Process myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("Process_StandardOutput_Sample.exe"
 );
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();

StreamReader myStreamReader = myProcess.StandardOutput;
// Read the standard output of the spawned process.
string myString = myStreamReader.ReadLine();
Console.WriteLine(myString);
myProcess.Close();
Process^ myProcess = gcnew Process;
ProcessStartInfo^ myProcessStartInfo = gcnew ProcessStartInfo(
   "Process_StandardOutput_Sample.exe" );
myProcessStartInfo->UseShellExecute = false;
myProcessStartInfo->RedirectStandardOutput = true;
myProcess->StartInfo = myProcessStartInfo;
myProcess->Start();

StreamReader^ myStreamReader = myProcess->StandardOutput;
// Read the standard output of the spawned process.
String^ myString = myStreamReader->ReadLine();
Console::WriteLine( myString );
myProcess->Close();
.NET Framework のセキュリティ.NET Framework のセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
Process クラス
Process メンバ
System.Diagnostics 名前空間
Process.StandardInput プロパティ
Process.StandardError プロパティ
ProcessStartInfo.RedirectStandardOutput



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「Process.StandardOutput プロパティ」の関連用語

Process.StandardOutput プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Process.StandardOutput プロパティのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS