1. 要加入命名空间   using System.Net.NetworkInformation

2.下面给出测试网络是否正常的方法代码

private void checkNetConnect()
        {
            string url = "www.baidu.com"; //设置网址为百度测试网址
                 
            string state = "";
            Ping ping = new Ping();

            //此处用try catch作为判断是因为有时候会有延迟,只要出现异常则认为是未连接状态
            try
            {
                PingReply reply =ping.Send(url);
                if (reply.Status==IPStatus.Success)
                {
                    this.pictureBox3.Image=Test.Properties.Resources.connected; //此处连接正常是图片的显示
                    state = "Netconnect Normal";
                    this.label1.Text = state;           //label提示信息
                }
                     if (reply.Status==IPStatus.TimedOut)
                {
                    this.pictureBox3.Image = Test.Properties.Resources.noconnected;
                    state = "Netconnect Abnormal";
                    this.label1.Text = state;
                }
            }
            catch (Exception ex)
            {
                string exceptions = ex.Message.ToString();

                throw new ApplicationException(exceptions);
            }
        }


本文转载:CSDN博客