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

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

封装函数代码:

/// <summary>
/// 通过访问域名判断网络
/// </summary>
/// <returns></returns>
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、

posted @ 2021-08-31 15:12  QianTM  阅读(294)  评论(0编辑  收藏  举报