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. 运行结果:

posted on 2019-03-18 15:42  在代码的世界里游走  阅读(1914)  评论(0编辑  收藏  举报