C# -- 使用Ping检查网络是否正常
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. 运行结果: