国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

使用C#調(diào)用外部Ping命令獲取網(wǎng)絡(luò)連接情況

2024-07-21 02:19:17
字體:
供稿:網(wǎng)友
以前在玩windows 98的時候,幾臺電腦連起來,需要測試網(wǎng)絡(luò)連接是否正常,經(jīng)常用的一個命令就是ping.exe。感覺相當(dāng)實(shí)用。

現(xiàn)在 .net為我們提供了強(qiáng)大的功能來調(diào)用外部工具,并通過重定向輸入、輸出獲取執(zhí)行結(jié)果,下面就用一個例子來說明調(diào)用ping.exe命令實(shí)現(xiàn)網(wǎng)絡(luò)的檢測,希望對.net初學(xué)者有所幫助。

首先,我們用使用process類,來創(chuàng)建獨(dú)立的進(jìn)程,導(dǎo)入system.diagnostics,

using system.diagnostics;

實(shí)例一個process類,啟動一個獨(dú)立進(jìn)程

process p = new process();

process類有一個startinfo屬性,這個是processstartinfo類,包括了一些屬性和方法,

下面我們用到了他的幾個屬性:

設(shè)定程序名

p.startinfo.filename = "cmd.exe";

關(guān)閉shell的使用

p.startinfo.useshellexecute = false;

重定向標(biāo)準(zhǔn)輸入

p.startinfo.redirectstandardinput = true;

重定向標(biāo)準(zhǔn)輸出

p.startinfo.redirectstandardoutput = true;

重定向錯誤輸出

p.startinfo.redirectstandarderror = true;

設(shè)置不顯示窗口

p.startinfo.createnowindow = true;

上面幾個屬性的設(shè)置是比較關(guān)鍵的一步。

既然都設(shè)置好了那就啟動進(jìn)程吧,

p.start();

輸入要執(zhí)行的命令,這里就是ping了,

p.standardinput.writeline("ping -n 1 192.192.132.229");

p.standardinput.writeline("exit");

從輸出流獲取命令執(zhí)行結(jié)果,

string strrst = p.standardoutput.readtoend();

在本機(jī)測試得到如下結(jié)果:

"microsoft windows 2000 [version 5.00.2195]/r/n(c) 版權(quán)所有 1985-2000 microsoft corp./r/n/r/nd://himuraz//csharpproject//zz//consoletest//bin//debug>ping -n 1 192.192.132.231/r/n/r/r/npinging 192.192.132.231 with 32 bytes of data:/r/r/n/r/r/nreply from 192.192.132.231: bytes=32 time<10ms ttl=128/r/r/n/r/r/nping statistics for 192.192.132.231:/r/r/n packets: sent = 1, received = 1, lost = 0 (0% loss),/r/r/napproximate round trip times in milli-seconds:/r/r/n minimum = 0ms, maximum = 0ms, average = 0ms/r/r/n/r/nd://himuraz//csharpproject//zz//consoletest//bin//debug>exit/r/n"

有了輸出結(jié)果,那還有什么好說的,分析strrst字符串就可以知道網(wǎng)絡(luò)的連接情況了。

下面是一個完整的程序,當(dāng)然對ping.exe程序執(zhí)行的結(jié)果不全,讀者可以進(jìn)一步修改

完整代碼如下:

using system;

using system.diagnostics;

namespace zz

{

class zzconsole

{

[stathread]

static void main(string[] args)

{

string ip = "192.192.132.229";

string strrst = cmdping(ip);

console.writeline(strrst);

console.readline();

}

private static string cmdping(string strip)

{

process p = new process();

p.startinfo.filename = "cmd.exe";

p.startinfo.useshellexecute = false;

p.startinfo.redirectstandardinput = true;

p.startinfo.redirectstandardoutput = true;

p.startinfo.redirectstandarderror = true;

p.startinfo.createnowindow = true;

string pingrst;

p.start();

p.standardinput.writeline("ping -n 1 "+strip);

p.standardinput.writeline("exit");

string strrst = p.standardoutput.readtoend();

if(strrst.indexof("(0% loss)")!=-1)

pingrst = "連接";

else if( strrst.indexof("destination host unreachable.")!=-1)

pingrst = "無法到達(dá)目的主機(jī)";

else if(strrst.indexof("request timed out.")!=-1)

pingrst = "超時";

else if(strrst.indexof("unknown host")!=-1)

pingrst = "無法解析主機(jī)";

else

pingrst = strrst;

p.close();

return pingrst;

}

}

}




總結(jié),這里就是為了說明一個問題,不但是ping命令,只要是命令行程序或者是dos內(nèi)部命令,我們都可以用上面的方式來執(zhí)行它,并獲取相應(yīng)的結(jié)果,并且這些程序的執(zhí)行過程不會顯示出來,如果需要調(diào)用外部程序就可以嵌入到其中使用了。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 容城县| 龙江县| 金昌市| 怀远县| 怀宁县| 陕西省| 遂宁市| 元氏县| 渝北区| 临清市| 深圳市| 桐柏县| 莱西市| 乌拉特中旗| 锡林浩特市| 台东市| 兴隆县| 浪卡子县| 犍为县| 托里县| 腾冲县| 沛县| 确山县| 九龙县| 嫩江县| 武义县| 织金县| 名山县| 南丹县| 宜都市| 五河县| 黎平县| 香格里拉县| 玛纳斯县| 吉隆县| 疏附县| 洛浦县| 乌鲁木齐县| 南安市| 井陉县| 麻城市|