当前位置: 首页 > news >正文

C#怎么判断电脑是否联网

在 C# 中,可以通过几种方法检测计算机是否联网。以下是几种常用的方式:

1. 使用 System.Net.NetworkInformation.Ping

通过发送一个 Ping 请求到公共 DNS 服务器(如 Google 的 DNS 8.8.8.8)来检测是否联网。这是最常见的一种方法,适用于大部分场景。

using System;
using System.Net.NetworkInformation;class Program
{static void Main(string[] args){if (IsConnectedToInternet()){Console.WriteLine("已连接到互联网");}else{Console.WriteLine("未连接到互联网");}}public static bool IsConnectedToInternet(){try{using (Ping ping = new Ping()){PingReply reply = ping.Send("8.8.8.8", 1000); // Ping Google DNSreturn reply.Status == IPStatus.Success;}}catch (PingException){return false;}}
}

2. 使用 System.Net.NetworkInformation.NetworkInterface 检查网络状态

此方法可以检查计算机是否有启用的网络接口并且该接口是否连接到网络。你可以遍历所有网络接口并检查其状态。

using System;
using System.Net.NetworkInformation;class Program
{static void Main(string[] args){if (IsConnectedToInternet()){Console.WriteLine("已连接到互联网");}else{Console.WriteLine("未连接到互联网");}}public static bool IsConnectedToInternet(){bool isConnected = false;foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces()){if (netInterface.OperationalStatus == OperationalStatus.Up && netInterface.NetworkInterfaceType != NetworkInterfaceType.Loopback){isConnected = true;break;}}return isConnected;}
}

3. 使用 System.Net.NetworkInformation.NetworkInterface 结合 DNS 查询

如果只是想知道是否有可用的网络连接,可以结合 DNS 查询来确定是否可以访问外部网站:

using System;
using System.Net.NetworkInformation;
using System.Net;class Program
{static void Main(string[] args){if (IsConnectedToInternet()){Console.WriteLine("已连接到互联网");}else{Console.WriteLine("未连接到互联网");}}public static bool IsConnectedToInternet(){try{// 尝试访问公共的 DNS 服务(如 Google 的 8.8.8.8)Dns.GetHostEntry("www.google.com");return true;}catch (Exception){return false;}}
}

4. 使用 Windows.Networking.Connectivity.NetworkInformation(UWP 应用)

如果你的应用是一个 UWP (Universal Windows Platform) 应用程序,你可以使用 Windows.Networking.Connectivity.NetworkInformation 类来检查网络状态。

using System;
using Windows.Networking.Connectivity;class Program
{static void Main(string[] args){if (IsConnectedToInternet()){Console.WriteLine("已连接到互联网");}else{Console.WriteLine("未连接到互联网");}}public static bool IsConnectedToInternet(){var internetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();return internetConnectionProfile != null && internetConnectionProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;}
}

5. 通过 HttpWebRequestHttpClient 请求

如果想要更精确地判断是否能够进行 HTTP 请求,特别是在需要验证特定服务是否在线时,可以使用 HttpWebRequestHttpClient 来尝试连接互联网。

using System;
using System.Net.Http;
using System.Threading.Tasks;class Program
{static async Task Main(string[] args){if (await IsConnectedToInternet()){Console.WriteLine("已连接到互联网");}else{Console.WriteLine("未连接到互联网");}}public static async Task<bool> IsConnectedToInternet(){try{using (HttpClient client = new HttpClient()){// 尝试访问 Google 网站HttpResponseMessage response = await client.GetAsync("http://www.google.com");return response.IsSuccessStatusCode;}}catch (Exception){return false;}}
}

总结:

  • Ping 请求:最常用的方式,简单且有效。
  • 网络接口检查:适合检查是否有启用的网络接口。
  • DNS 查询:通过 DNS 查询也能确认网络连接是否可用。
  • HttpWebRequest:用于验证网络是否可访问特定网站或服务。
http://www.lryc.cn/news/499228.html

相关文章:

  • 软件体系结构复习-02 软件体系结构定位及构建
  • 鸿蒙获取 APP 信息及手机信息
  • Linux-V4L2摄像头应用编程
  • 掌握谈判技巧,达成双赢协议
  • Mysql - 存储引擎
  • 借助 CC-Link IE FB 转 Profinet 网关实现西门子 PLC 接入三菱 PLC 系统的解决策略
  • 未完成_RFdiffusion应用案例_从头设计pMHC的结合剂
  • python使用h5py保存数据
  • ubuntu24.04利用selenium控制浏览器的方法
  • Thonny IDE + MicroPython + ESP32 + 0.96寸OLED(IIC) 显示任意字符
  • centos7 扩容
  • FreeRTOS实现UART通信
  • 【从CURD到全栈成长】Java后端如何提升自己?
  • 动态计算加载图片
  • 利用R包QstFstComp包进行Qst-Fst分析
  • ASP.NET Core8.0学习笔记(二十五)——EF Core Include导航数据加载之预加载与过滤
  • ubuntu常用的设置
  • 基于框架的逻辑回归:原理、实现与应用
  • Charts 教程:创建交互式图表的基础
  • VTK知识学习(20)- 数据的存储与表达
  • springboot网站开发-使用redis作为定时器控制手机号每日注册次数
  • IntelliJ+SpringBoot项目实战(28)--整合Beetl模板框架
  • Kafka-Connect源码分析
  • 项目五 李白个人生平(资源)
  • 计算机视觉与各个学科融合:探索新方向
  • 数据分析类论文通过stata进行数据预处理(一)
  • 力扣——1.返回字符串中第一个唯一的字符;2.把字符串转换成整数(C++)
  • M-LAG【根桥方式】
  • 新书速览|循序渐进Node.js企业级开发实践
  • Xlsxwriter生成Excel文件时TypeError异常处理