[英]How to determine if network interface is connected to the Internet (i. e. the computer is online)
I need a way to determine internet availability programmatically. 我需要一种以编程方式确定互联网可用性的方法。 At now i use Ping to constantly ping some internet site.
现在我用Ping来不断ping一些网站。
But it seems Windows 7 though determines internet availability in some other way. 但似乎Windows 7虽然以其他方式确定了互联网可用性。 If computer is online there is earth icon on the network interface icon in the System Tray.
如果计算机处于联机状态,则系统托盘中的网络接口图标上会显示地球图标。
The question is: is there any standard Win32 way to check online status, win event or something, and if so, how to use it from C#? 问题是:是否有任何标准的Win32方式来检查在线状态,赢取事件或其他什么,如果有,如何从C#中使用它?
I believe something like this would work although your question appears to be a duplicate: 虽然你的问题似乎是重复的,但我相信这样的事情会奏效:
using System;
using System.Runtime;
using System.Runtime.InteropServices;
public class InternetCS
{
//Creating the extern function...
[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( out int Description, int ReservedValue );
//Creating a function that uses the API function...
public static bool IsConnectedToInternet( )
{
int Desc ;
return InternetGetConnectedState( out Desc, 0 ) ;
}
}
forund here: 在这里:
check whether Internet connection is available with C# 检查C#是否可以使用Internet连接
'Connected to the Internet' doesn't have any actual meaning except in the case of a modem. 除了调制解调器之外,“连接到互联网”没有任何实际意义。 The beat way to test whether any resource is available is to use it.
测试任何资源是否可用的节拍方法是使用它。 You have to cope with failures at that point anyway, no need to code everything twice.
无论如何,你必须应对失败,不需要对所有内容进行两次编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.