使用C#調用外部Ping命令擷取網路連接情況

來源:互聯網
上載者:User
使用C#調用外部Ping命令擷取網路連接情況

以前在玩Windows 98的時候,幾台電腦連起來,需要測試網路連接是否正常,經常用的一個命令就是Ping.exe。感覺相當實用。

現在 .net為我們提供了強大的功能來調用外部工具,並通過重新導向輸入、輸出擷取執行結果,下面就用一個例子來說明調用Ping.exe命令實現網路的檢測,希望對.net初學者有所協助。

首先,我們用使用Process類,來建立獨立的進程,匯入System.Diagnostics,

using System.Diagnostics;

執行個體一個Process類,啟動一個獨立進程

Process p = new Process();

Process類有一個StartInfo屬性,這個是ProcessStartInfo類,包括了一些屬性和方法,

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

設定程式名

p.StartInfo.FileName = "cmd.exe";

關閉Shell的使用

p.StartInfo.UseShellExecute = false;

重新導向標準輸入

p.StartInfo.RedirectStandardInput = true;

重新導向標準輸出

p.StartInfo.RedirectStandardOutput = true;

重新導向錯誤輸出

p.StartInfo.RedirectStandardError = true;

設定不顯示視窗

p.StartInfo.CreateNoWindow = true;

上面幾個屬性的設定是比較關鍵的一步。

既然都設定好了那就啟動進程吧,

p.Start();

輸入要執行的命令,這裡就是ping了,

p.StandardInput.WriteLine("ping -n 1 192.192.132.229");

p.StandardInput.WriteLine("exit");

從輸出資料流擷取命令執行結果,

string strRst = p.StandardOutput.ReadToEnd();

在本機測試得到如下結果:

"Microsoft Windows 2000 [Version 5.00.2195]\r\n(C) 著作權 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"

有了輸出結果,那還有什麼好說的,分析strRst字串就可以知道網路的串連情況了。

下面是一個完整的程式,當然對Ping.exe程式執行的結果不全,讀者可以進一步修改

完整代碼如下:

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 = "無法到達目的主機";

else if(strRst.IndexOf("Request timed out.")!=-1)

pingrst = "逾時";

else if(strRst.IndexOf("Unknown host")!=-1)

pingrst = "無法解析主機";

else

pingrst = strRst;

p.Close();

return pingrst;

}

}

}

總結,這裡就是為了說明一個問題,不但是Ping命令,只要是命令列程式或者是Dos內部命令,我們都可以用上面的方式來執行它,並擷取相應的結果,並且這些程式的執行過程不會顯示出來,如果需要調用外部程式就可以嵌入到其中使用了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.