2013-02-08 131 views
0

這是我編寫的打開cmd的程序,運行IPERF作爲客戶端。 與服務器連接後,應該顯示網絡的帶寬。 但是,命令提示符關閉,我需要的信息僅在服務器端可用。通過Visual Studio(C#)打開命令提示符,運行一些命令,然後通過文本框在命令提示符中顯示結果詳細信息。

如何獲取信息並將其顯示在MessageBox中?

任何形式的援助是非常感謝。

string output; 
ProcessStartInfo start = new ProcessStartInfo(@"C:\Windows\System32\cmd.exe"); 
start.UseShellExecute = false; 
start.ErrorDialog = false; 
start.WindowStyle = ProcessWindowStyle.Normal; 
start.RedirectStandardError = true; 
start.RedirectStandardInput = true; 
start.RedirectStandardOutput = true; 
Process cmd = new Process(); 
cmd.StartInfo = start; 
cmd.Start(); 
try 
{ 
    Process.Start("cmd", "/C iperf -c " + IP_Address); 
} 
catch 
{ 
} 
Thread.Sleep(1000); 
StreamReader outputReader = cmd.StandardOutput; 
StreamReader errorReader = cmd.StandardError;   
output = outputReader.ReadToEnd(); 
MessageBox.Show(output); 
+1

爲什麼你必須運行「CMD」,你嘗試過直接運行的進程? – ChrisBint

+0

您提到「服務器端」....如果這是客戶端/服務器,爲什麼要使用'MessageBox'?這很可能是這裏的問題......運行此代碼的是哪種類型的應用程序? –

+0

另外:'睡眠(1000)'是可以避免的;你可以使用'cmd.WaitForExit(1000)',例如 –

回答

0

try { Process.Start("cmd", "/C iperf -c " + IP_Address); }

您是否嘗試過 「CMD/K」?它應該防止終端在你的論點完成後關閉。

try { Process.Start("cmd /k", "/C iperf -c " + IP_Address); }