网站域名注册的相关证书证明文件,网站编辑楼盘详情页怎么做,手机网站怎么导入微信朋友圈,靖江网站建设价格在 C# 中#xff0c;可以通过几种方法检测计算机是否联网。以下是几种常用的方式#xff1a;
1. 使用 System.Net.NetworkInformation.Ping 类
通过发送一个 Ping 请求到公共 DNS 服务器#xff08;如 Google 的 DNS 8.8.8.8#xff09;来检测是否联网。这是最常见的一种…在 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.8Dns.GetHostEntry(www.google.com);return true;}catch (Exception){return false;}}
}4. 使用 Windows.Networking.Connectivity.NetworkInformationUWP 应用
如果你的应用是一个 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. 通过 HttpWebRequest 或 HttpClient 请求
如果想要更精确地判断是否能够进行 HTTP 请求特别是在需要验证特定服务是否在线时可以使用 HttpWebRequest 或 HttpClient 来尝试连接互联网。
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 Taskbool 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用于验证网络是否可访问特定网站或服务。