2013-05-09 2 views
2

콘솔 프로세스의 표준 출력을 시작한 다음 읽는 응용 프로그램이 있습니다. 그 콘솔 프로세스에서 콘솔에 쓰는 DLL 파일을 호출합니다.하지만 그 메시지를 캡처하고 싶지는 않습니다. 출력하는 문자열을 캡처하고 싶습니다.콘솔 프로세스에서 한 줄만 캡처하는 방법은 무엇입니까?

나는 일을 시도 :

exeProcess.BeginOutputReadLine(); 
       string errString = exeProcess.StandardError.ReadToEnd(); 

당신이 나에게이 대안을 줄 수 :

verboseMethod(); //method writting things into the console 

output = dllMethod(); //method returning what I want 

Console.Clear(); 
Console.Out.Write(output) 

나는이 그래서 Console.Clear()를 실행하기 전에 모든 것을 읽고있다 생각하고 있는가? 마지막 출력 메시지가 나올 때까지 기다리는 것과 비슷합니까?

편집

나는 (있는 경우)이 같은 도움이 될 생각 .. 내가 출력을 리디렉션되지 않았거나 특정 지점에서 아무것도를 작성하고 다음 쓸 수 있도록하지 콘솔을 알 수 있습니다 코드의 다른 곳에서 다시 한번? 마찬가지로 :

Console.CloseBuffer(); 

Console.OpenBuffer(); 
+0

출력을 스트림으로 리디렉션 한 다음 필요에 따라 해당 스트림에서 읽기 ("캡처")를 중지하는 것입니다. – redtuna

답변

0

당신은 출력의 asychronous 리디렉션을 사용하여 시도하고 켜져 야합니다 그리고 당신은 당신이 원하는 실행 사이에 촬영되도록 'EnableRaisingEvents'끕니다. BeginOutputReadLine() 및 CancelOutputRead()를 사용하여 콘솔 읽기를 시작/중지하십시오.

 var myOutput = new StringBuilder(); 
     var myProcess = new Process(); 
     myProcess.StartInfo = new ProcessStartInfo(path, command); 
     myProcess.StartInfo.UseShellExecute = false; 
     myProcess.StartInfo.RedirectStandardOutput = true; 
     myProcess.StartInfo.RedirectStandardError = true; 
     myProcess.EnableRaisingEvents = true; 

     myProcess.OutputDataReceived += (object sendingProcess, DataReceivedEventArgs e) => 
     { 
      if (e.Data != null) 
      { 
       myOutput.AppendLine(e.Data); 
      } 
     }; 

     myProcess.ErrorDataReceived += (object sendingProcess, DataReceivedEventArgs e) => 
     { 
      if (e.Data != null) 
      { 
       myOutput.AppendLine(e.Data); 
      } 
     }; 
     myProcess.Start(); 

     verboseMethod(); 

     //Start Capture here! 
     myProcess.BeginErrorReadLine(); 
     myProcess.BeginOutputReadLine(); 

     dllMethod(); 
관련 문제