下面以ping 為例用到命名空間System.Diagnostics;
System.Diagnostics 命名空間 包含了能夠與系統(tǒng)進(jìn)程 事件日志 和性能計(jì)數(shù)器進(jìn)行交互的類(lèi) 一般用于幫助診斷和調(diào)試應(yīng)用程序 例如 Debug類(lèi)用于幫組調(diào)試代碼 Process類(lèi)能夠控制進(jìn)程訪問(wèn) Trace類(lèi)能夠跟蹤代碼的執(zhí)行情況
Process 用于操作本地或者遠(yuǎn)程進(jìn)程打訪問(wèn) 通過(guò)Process 可以在托管環(huán)境下很容易的操作對(duì)外部進(jìn)程的啟動(dòng)或者停止 。
復(fù)制代碼代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace TestEqual
{
class Program
{
static void Main(string[] args)
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = "iexplore.exe";
myProcess.StartInfo.Arguments = "http://www.baidu.com";
myProcess.Start();
}
}
}
必須設(shè)置相應(yīng)的FileName和Arguments屬性
下面以ping為例
代碼如下:
復(fù)制代碼代碼如下:
string hostname = "http://www.baidu.com"; //或者這里是ip等;
Process prc=new Process();
prc.StartInfo.FileName="cmd.exe";
prc.StartInfo.UseShellExecute=false;
prc.StartInfo.RedirectStandardInput = true;
prc.StartInfo.RedirectStandardOutput = true;
prc.StartInfo.RedirectStandardError = true;
prc.StartInfo.CreateNoWindow = false;
prc.Start();
prc.StandardInput.WriteLine("ping " + hostname);
prc.StandardInput.Close();
Response.Write(prc.StandardOutput.ReadToEnd());
這里還可以調(diào)用很多命令自己可以研究下