2013-07-23 3 views
2

나는 3-4 버전을 시도하고 아무도 나를 위해 일했다. 나는 더 이상 소스가 없지만 그것은 잘 작동하지만, 나는 여전히 그것에서 결과 문자열을 필요로 작은 exe를 가지고캔트 프로세스에서 표준 출력을 리디렉션

Process proc = new Process(); 
ProcessStartInfo StartInfo = new ProcessStartInfo(); 
StartInfo.RedirectStandardError = true; 
StartInfo.RedirectStandardOutput = true; 
StartInfo.FileName = path + "calculate.exe"; 
StartInfo.Arguments = input; 
StartInfo.UseShellExecute = false; 
StartInfo.CreateNoWindow = true; 

proc.StartInfo = StartInfo; 

proc.Start(); 

proc.WaitForExit(); 

string output = proc.StandardOutput.ReadToEnd(); 

MessageBox.Show(output); 

빈 메시지 박스가 생깁니다. 이유가 무엇입니까? WaitForExit에 중단 점을 추가하면 StandardOutput에서 리디렉션되지 않았거나 프로세스가 아직 시작되지 않았다고 말합니다.

답변

4

RedirectStandardError이 아니라 RedirectStandardOutput으로 설정해야합니다.
(또한 프로세스가 표준 출력이 아닌 표준 출력에 실제로 쓰는지 확인하십시오.)

+0

젠장 자동 완성! 하지만 그래도 맞아, 난 여전히 비어있는 메시지 상자가 생겼어 .. –

+0

코드가 업데이트되었는데 여전히 작동하지 않는다. –

+0

@AdnanElezovic'proc.StandardError.ReadToEnd();도 시도해 봤나? 프로그램이 stderr로 출력 할 수 있습니다. – Gray

0

시도해 보셨습니까?

// This is the code for the base process 
     Process myProcess = new Process(); 
     // Start a new instance of this program but specify the 'spawned' version. 
     ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(args[0], "spawn"); 
     myProcessStartInfo.UseShellExecute = false; 
     myProcessStartInfo.RedirectStandardOutput = true; 
     myProcess.StartInfo = myProcessStartInfo; 
     myProcess.Start(); 
     StreamReader myStreamReader = myProcess.StandardOutput; 
     // Read the standard output of the spawned process. 
     string myString = myStreamReader.ReadLine(); 
     Console.WriteLine(myString); 

     myProcess.WaitForExit(); 
     myProcess.Close(); 

출처 : MSDN

관련 문제