C#使用ping命令的兩個例子
更新時間:2014年08月30日 12:12:49 投稿:junjie
這篇文章主要介紹了C#使用ping命令的兩個例子,本文給出了調(diào)用CMD命令和使用C# ping類的兩個例子,需要的朋友可以參考下
方法一:調(diào)用cmd 的ping命令
private static string CmdPing(string strIp) { Process p = new Process(); p.StartInfo.FileName = "cmd.exe";//設(shè)定程序名 p.StartInfo.UseShellExecute = false; //關(guān)閉Shell的使用 p.StartInfo.RedirectStandardInput = true;//重定向標(biāo)準(zhǔn)輸入 p.StartInfo.RedirectStandardOutput = true;//重定向標(biāo)準(zhǔn)輸出 p.StartInfo.RedirectStandardError = true;//重定向錯誤輸出 p.StartInfo.CreateNoWindow = true;//設(shè)置不顯示窗口 string pingrst; p.Start(); p.StandardInput.WriteLine("ping " + 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; }
方法二:使用C#中的ping 類
private void displayReply(PingReply reply) //顯示結(jié)果 { Ping p1 = new Ping(); //只是演示,沒有做錯誤處理 PingReply reply = p1.Send("填寫ip地址"); StringBuilder sbuilder ; if (reply.Status == IPStatus.Success) { sbuilder = new StringBuilder(); sbuilder.Append(string.Format("Address: {0} ", reply.Address.ToString ())); sbuilder.Append(string.Format("RoundTrip time: {0} ", reply.RoundtripTime)); sbuilder.Append(string.Format("Time to live: {0} ", reply.Options.Ttl)); sbuilder.Append(string.Format("Don't fragment: {0} ", reply.Options.DontFragment)); sbuilder.Append(string.Format("Buffer size: {0} ", reply.Buffer.Length)); response.write(sbuilder.ToString()); } else if (reply.Status == IPStatus.TimeOut) { response.write("超時"); }else{ response.write("失敗"); }
您可能感興趣的文章:
- 如何根據(jù)百度地圖計算出兩地之間的駕駛距離(兩種語言js和C#)
- C#實現(xiàn)百度網(wǎng)站收錄和排名查詢功能思路及實例
- C#實現(xiàn)解析百度天氣數(shù)據(jù),Rss解析百度新聞以及根據(jù)IP獲取所在城市的方法
- c#封裝百度web服務(wù)geocoding api 、百度坐標(biāo)轉(zhuǎn)換示例
- asp.net c# 調(diào)用百度pai實現(xiàn)在線翻譯,英文轉(zhuǎn)中文
- C# ping網(wǎng)絡(luò)IP 實現(xiàn)網(wǎng)絡(luò)狀態(tài)檢測的方法
- C#判斷ip地址是否可以ping的通
- C#實現(xiàn)百度ping推送功能的方法
相關(guān)文章
C#使用DateTime.Now靜態(tài)屬性動態(tài)獲得系統(tǒng)當(dāng)前日期和時間
本文主要介紹了C#使用DateTime.Now靜態(tài)屬性動態(tài)獲得系統(tǒng)當(dāng)前日期和時間,DateTime結(jié)構(gòu)的Now靜態(tài)屬性只是得到一個系統(tǒng)時間對象,該時間對象不會隨著系統(tǒng)時間的變化而變化,如果要動態(tài)顯示系統(tǒng)時間,可以使用計時器間隔地獲取系統(tǒng)時間對象并顯示,感興趣的可以了解一下2024-01-01分享我在工作中遇到的多線程下導(dǎo)致RCW無法釋放的問題
最近在做項目中遇到一個問題,在調(diào)用一個類庫中的方法時,出現(xiàn)如下異常信息:嘗試釋放正在使用的RCW,活動線程或其他線程上正在使用該 RCW,釋放正在使用的 RCW 的嘗試會導(dǎo)致?lián)p壞或數(shù)據(jù)丟失2015-12-12