C#判断本地电脑网络状态


1、通过访问域名判断网络

封装函数代码:

/// 
/// 通过访问域名判断网络
/// 
/// 
public static bool IsInternetAvailable()
{
     try
     {
           Dns.GetHostEntry("www.baidu.com"); //using System.Net;   //www.google.com
           return true;
      }
      catch (SocketException ex)
      {
           return false;
      }
}

调用:

if (!IsInternetAvailable()) //判断是否存在网络
{
    MessageBox.Show("网络未连接");
}

2、