using System.Diagnostics ;
using System.IO;
private void btnRun_Click(object sender, EventArgs e)
{
txtResult.Text = "";
processCommand("Ping.exe", this.txtAddress.Text);
processCommand("Ping.exe", this.txtAddress.Text);
}
public void processCommand(string commandName, string argument)
{
ProcessStartInfo start = new ProcessStartInfo(commandName);//設置運行的命令行文件問ping.exe文件,這個文件系統會自己找到
//如果是其它exe文件,則有可能需要指定詳細路徑,如運行winRar.exe
start.WorkingDirectory = "d:\360Downloads\";
start.Arguments = argument;//設置命令參數
start.CreateNoWindow = true;//不顯示dos命令行窗口
start.RedirectStandardOutput = true;//
start.RedirectStandardInput = true;//
start.UseShellExecute = false;//是否指定操作系統外殼進程啟動程序
txtResult.AppendText(start.WorkingDirectory + "
");
Process p = Process.Start(start);
StreamReader reader = p.StandardOutput;//截取輸出流
string line = reader.ReadLine();//每次讀取一行
while (!reader.EndOfStream)
{
txtResult.AppendText(line + "
");
line = reader.ReadLine();
}
p.WaitForExit();//等待程序執行完退出進程
p.Close();//關閉進程
reader.Close();//關閉流
}