C#Winfrom 验证当前网络是否通常

来源:互联网 发布:淘宝论坛官网 编辑:程序博客网 时间:2024/06/07 20:53

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);            }        }

0 0
原创粉丝点击