C# 如何知道MS Windows是否有活动的红色或internet连接?
可能重复:C# 如何知道MS Windows是否有活动的红色或internet连接?,c#,.net,C#,.net,可能重复: 我只想知道如果微软Windows有或没有活动的Internet/red连接,我们必须使用哪些方法以编程方式(C#)检测 有可能吗 谢谢大家! 例如:如果我放下WIFI,我如何知道有任何连接?查看 我使用以下方法检查连接: public bool CheckForInternetConnection() { try { using (var client = new WebClient()) us
我只想知道如果微软Windows有或没有活动的Internet/red连接,我们必须使用哪些方法以编程方式(C#)检测 有可能吗 谢谢大家! 例如:如果我放下WIFI,我如何知道有任何连接?查看
我使用以下方法检查连接:
public bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead("http://www.google.com"))
{
Console.WriteLine("You Have connectity!");
return true;
}
}
catch
{
Console.WriteLine("No internet connection found.");
return false;
}
}
该方法尝试加载Google的URL,如果加载则返回true,否则返回false 你认为Ping方法更快吗?或者?不知道原始速度-此方法返回的结果足以满足我的目的。这一切取决于你正在阅读的网站有多大is@MicahArmantrout事实上,我必须检查Web服务是否处于活动状态。为什么不尝试连接并封装在一个try-catch中呢?类似的:我也找到了这个答案,这个答案也很好!ICMP ping被某些路由器/配置阻止。
public bool CheckForInternetConnection()
{
try
{
using (var client = new WebClient())
using (var stream = client.OpenRead("http://www.google.com"))
{
Console.WriteLine("You Have connectity!");
return true;
}
}
catch
{
Console.WriteLine("No internet connection found.");
return false;
}
}