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.
But it seems Windows 7 though determines internet availability in some other way. 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#?
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#
'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.
精彩评论