2013-01-07 3 views
0

의 표준 출력을 읽기 그것은 내가 어떻게 읽어야하는 응용 프로그램이 내 자신의 응용 프로그램

Process p = Process.GetCurrentProcess(); 
StreamReader input = p.StandardOutput; 
input.ReadLine(); 

을 시도하고있다

Console.WriteLine("blah blah"); 

를 통해 기록 된 자신의 출력이다 그러나 그것은 작동하지 않습니다 두 번째 줄에서 "InvalidOperationException"때문에. "StandardOutput이 리디렉션되지 않았거나 프로세스가 아직 시작되지 않았습니다."와 같은 메시지가 표시됩니다.

내 자신의 출력을 읽으려면 어떻게해야합니까? 거기에 또 다른 방법이 있을까요? 그리고 내 자신의 입력을 작성하는 방법을 완벽하게?

출력이있는 응용 프로그램이 이미 실행 중입니다.

출력을 읽고 싶습니다. 동일한 응용 프로그램에 있습니다. 2nd 앱이 없습니다. 오직 하나.

+0

정확히하고 싶습니까? –

+1

콘솔에 쓰고 필요한 추가 작업을 수행하는 방법을 쓰지 않는 이유는 무엇입니까? – boindiil

+3

정말 호기심 ... 왜 그렇게하고 싶습니까? – Moriya

답변

2

의도가 무엇인지 추측하고 있지만 시작한 응용 프로그램의 출력을 읽으려면 출력을 리디렉션 할 수 있습니다.

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

예를 http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx

편집에서 :

당신이 당신의 편집 사용할 수있는 지정으로 현재 콘솔 응용 프로그램의 출력을 재 지정하려면

.

private static void Main(string[] args) 
{ 
    StringWriter writer = new StringWriter(); 
    Console.SetOut(writer); 
    Console.WriteLine("hello world"); 

    StringReader reader = new StringReader(writer.ToString()); 
    string str = reader.ReadToEnd(); 
} 
+0

마지막 스레드를 버퍼링하고 비동기 적으로 만들면 다른 스레드에서 언제든지 읽을 수 있습니까? – Bitterblue

+0

당신은 그것을 시도해야하고 새로운 질문으로 돌아와야한다. – Moriya

관련 문제