2013-09-03 12 views
7

プロセスの標準出力を後で解析するための文字列にリダイレクトしたいと思います。 私はまた、プロセスが実行されている間、画面の出力が完了したときだけでなく、画面上の出力も見たいと思います。リダイレクトプロセス出力C#

これは可能ですか?

+0

実際に何をしようとしているのか詳細をご記入ください – Patel

+0

可能な複製の[C#実行中にプロセス出力を取得](http://stackoverflow.com/questions/11994610/c-sharp-get-process-output-実行中) –

答えて

15

RedirectStandardOutputを使用してください。 MSDNから

サンプル:

// 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(); 

はまた、より良いあなたの「プロセスの実行中に出力を参照してください」の要件を満たします ReadToEnd()の代替のための OutputDataReceivedBeginOutputReadLine()を参照してください。

0

あなたがC#アプリケーションからexeファイルを実行し、それからの出力を取得したいなら、あなたは真= p.EnableRaisingEventsを書くことforgeteないでください以下のコード

System.Diagnostics.Process p = new System.Diagnostics.Process();    

p.StartInfo.FileName = "PATH TO YOUR FILE"; 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.Arguments = metalType + " " + graphHeight + " " + graphWidth; 
p.StartInfo.CreateNoWindow = true; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.RedirectStandardError = true;    

p.EnableRaisingEvents = true; 
p.Start();    
svgText = p.StandardOutput.ReadToEnd(); 

using(StreamReader s = p.StandardError) 
{ 
    string error = s.ReadToEnd(); 
    p.WaitForExit(20000); 
} 

を使用することができます。