C# -- 使用Ping檢查網路是否正常 需引用命名空間: using System.Net.NetworkInformation; 1. 代碼實現 2. 運行結果: ...
C# -- 使用Ping檢查網路是否正常
需引用命名空間: using System.Net.NetworkInformation;
1. 代碼實現
try { List<string> listUrl = new List<string>() { "www.baidu.com", "www.sina.com", "www.taobao.com", "www.tencent.com" }; Ping ping = new Ping(); int iTimeOut = 3000; for (int i = 0; i < listUrl.Count; i++) { PingReply pingReply = ping.Send(listUrl[i], iTimeOut); Console.WriteLine("-----------------------------------------------------------------"); Console.WriteLine(string.Format("從{0}回覆的IP地址:{1}", listUrl[i], pingReply.Address)); Console.WriteLine(string.Format("ping耗時:{0}毫秒", pingReply.RoundtripTime)); Console.WriteLine(string.Format("ping結果:{0}", pingReply.Status)); } } catch (Exception EX) { Console.WriteLine(EX.Message); }
2. 運行結果: