2012-07-23 7 views
2

C#에서 명령 완료 후 명령 프롬프트 창의 내용을 얻으려고합니다.명령 완료 후 명령 프롬프트 창 내용 가져 오기

특히이 경우에는 버튼 클릭으로 ping 명령을 실행하고 출력을 텍스트 상자에 표시하려고합니다.

ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe"); 
startInfo.UseShellExecute = false; 
startInfo.RedirectStandardOutput = true; 
startInfo.RedirectStandardError = true; 
startInfo.Arguments = "ping 192.168.1.254"; 
Process pingIt = Process.Start(startInfo); 
string errors = pingIt.StandardError.ReadToEnd(); 
string output = pingIt.StandardOutput.ReadToEnd(); 

txtLog.Text = ""; 
if (errors != "") 
{ 
    txtLog.Text = errors; 
} 
else 
{ 
    txtLog.Text = output; 
} 

그리고 그것은 일종의 작품 :

내가 현재 사용하고있는 코드입니다. 그것은 적어도 일부 출력을 가져 와서 표시하지만, 핑 자체는 실행되지 않습니다. 또는 적어도 이것은 아래 출력과 명령 프롬프트 창이 잠깐 동안 깜박 인다고 가정합니다.

출력 :

마이크로 소프트 윈도우 [버전 6.1.7601] 저작권 (C) 2009 마이크로 소프트 공사. 판권 소유.

C : \ 체크 아웃 \ PingUtility \ PingUtility \ 빈 \ 디버그>

은 어떤 도움이 크게 감사합니다.

답변

6

이것은

ProcessStartInfo info = new ProcessStartInfo(); 
info.Arguments = "/C ping 127.0.0.1"; 
info.WindowStyle = ProcessWindowStyle.Hidden; 
info.CreateNoWindow = true; 
info.FileName = "cmd.exe"; 
info.UseShellExecute = false; 
info.RedirectStandardOutput = true; 
using (Process process = Process.Start(info)) 
{ 
    using (StreamReader reader = process.StandardOutput) 
    { 
     string result = reader.ReadToEnd(); 
     textBox1.Text += result; 
    } 
} 

출력

enter image description here

+0

아 완벽해야! 나는 최소한 일종의 오른쪽 라인을 따라 갔다. 감사! – LiamGu