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

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

ProcessStartInfo.RedirectStandardOutput プロパティ

アプリケーションの出力を Process.StandardOutput ストリームに書き込むかどうかを示す値を取得または設定します。

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

Public Property RedirectStandardOutput As
 Boolean
Dim instance As ProcessStartInfo
Dim value As Boolean

value = instance.RedirectStandardOutput

instance.RedirectStandardOutput = value
public bool RedirectStandardOutput { get;
 set; }
public:
property bool RedirectStandardOutput {
    bool get ();
    void set (bool value);
}
/** @property */
public boolean get_RedirectStandardOutput ()

/** @property */
public void set_RedirectStandardOutput (boolean
 value)
public function get RedirectStandardOutput
 () : boolean

public function set RedirectStandardOutput
 (value : boolean)

プロパティ値
出力を Process.StandardOutput に書き込む場合は true。それ以外の場合は false。

解説解説

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

メモメモ

RedirectStandardOutput を true に設定するには、UseShellExecute を false に設定する必要があります。設定されていない場合は、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 つのスレッドを作成して個別のスレッドで各ストリームの出力を読み取る方法もあります。

使用例使用例
compiler.StartInfo.FileName = "csc.exe"
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe
 stdstr.cs"
compiler.StartInfo.UseShellExecute = False
compiler.StartInfo.RedirectStandardOutput = True
compiler.Start()

Console.WriteLine(compiler.StandardOutput.ReadToEnd())

compiler.WaitForExit()
Process compiler = new Process();
compiler.StartInfo.FileName = "csc.exe";
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();    

Console.WriteLine(compiler.StandardOutput.ReadToEnd());

compiler.WaitForExit();
Process^ compiler = gcnew Process;
compiler->StartInfo->FileName = "cl.exe";
compiler->StartInfo->Arguments = "/clr stdstr.cpp /link /out:sample.exe";
compiler->StartInfo->UseShellExecute = false;
compiler->StartInfo->RedirectStandardOutput = true;
compiler->Start();

Console::WriteLine( compiler->StandardOutput->ReadToEnd() );

compiler->WaitForExit();
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
ProcessStartInfo クラス
ProcessStartInfo メンバ
System.Diagnostics 名前空間
UseShellExecute
Process.StandardOutput プロパティ



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「ProcessStartInfo.RedirectStandardOutput プロパティ」の関連用語

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

   

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



ProcessStartInfo.RedirectStandardOutput プロパティのページの著作権

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

©2026 GRAS Group, Inc.RSS