2013-09-03 5 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

당신이 다음 아래 코드

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); 
} 
을 사용할 수 있습니다에서 C# 응용 프로그램에서 EXE 파일을 실행하고 출력을 얻고 싶은 경우에

p.EnableRaisingEvents를 작성 forgete하지 마십시오 = TRUE;