C#實(shí)現(xiàn)Ping的方法小結(jié)
更新時(shí)間:2015年08月12日 12:33:56 作者:dyx2010
這篇文章主要介紹了C#實(shí)現(xiàn)Ping的方法,以?xún)蓚€(gè)實(shí)例形式形式較為詳細(xì)的分析了C#實(shí)現(xiàn)ping功能的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
本文實(shí)例總結(jié)了C#實(shí)現(xiàn)Ping的方法。分享給大家供大家參考。具體如下:
方法一:
class Program { public string cmdPing(string strIP) { Process myProcess = new Process(); myProcess.StartInfo.FileName = "cmd.exe"; myProcess.StartInfo.UseShellExecute = false; //要重定向 IO 流,Process 對(duì)象必須將 UseShellExecute 屬性設(shè)置為 False。 myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.RedirectStandardInput = true; myProcess.StartInfo.RedirectStandardError = true; string pingstr; myProcess.Start(); myProcess.StandardInput.WriteLine("ping " + strIP); myProcess.StandardInput.WriteLine("exit"); string strRst = myProcess.StandardOutput.ReadToEnd(); if (strRst.IndexOf("(0% loss)") != -1) pingstr = "連接"; else if (strRst.IndexOf("Destination host unreachable.") != -1) pingstr = "無(wú)法到達(dá)主機(jī)"; else if (strRst.IndexOf("UnKonw host") != -1) pingstr = "無(wú)法解析主機(jī)"; else pingstr = strRst; myProcess.Close(); return pingstr; } static void Main(string[] args) { Program myProgram = new Program(); string returnString = myProgram.cmdPing("127.0.0.1"); Console.WriteLine(returnString); Console.ReadLine(); } }
方法二:
static void Main(string[] args) { Ping ping = new Ping(); PingOptions pingOption = new PingOptions(50, true); string data = " you are a such a beautiful girl"; byte[] buffer = Encoding.ASCII.GetBytes(data); PingReply pingReply = ping.Send("192.168.1.100", 20, buffer); if (pingReply.Status == IPStatus.Success) { Console.WriteLine("address:{0}", pingReply.Address.ToString()); Console.WriteLine("Round Trip time {0}", pingReply.RoundtripTime); Console.WriteLine("time to live:{0}", pingReply.Options.Ttl); Console.WriteLine("Do not to fragement:{0}", pingReply.Options.DontFragment); } Console.ReadKey(); }
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)旋轉(zhuǎn)扭曲圖像特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02C#絕對(duì)路徑拼接相對(duì)路徑的實(shí)例代碼
C#絕對(duì)路徑拼接相對(duì)路徑的實(shí)例代碼,需要的朋友可以參考一下2013-03-03C# 使用匿名函數(shù)解決EventHandler參數(shù)傳遞的難題
C#動(dòng)態(tài)生成PictureBox并綁定右鍵菜單,實(shí)現(xiàn)刪除圖片2009-05-05C#實(shí)現(xiàn)xml文件的讀取與寫(xiě)入簡(jiǎn)單實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)xml文件的讀取與寫(xiě)入方法,涉及C#操作XML文件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08