How to determine if a network interface is connected to the Internet (i.e. a computer is connected to a network)

I need a way to programmatically determine the availability of the Internet. I am currently using Ping to constantly listen to an internet site.

But it seems that Windows 7, though, determines the availability of the Internet in some other way. If the computer is online, the earth icon is displayed on the network interface icon in the system tray.

Question: is there a standard way for Win32 to check online status, win an event or something like that, and if so, how to use it with C #?

+4
source share
2 answers

I believe something like this will work, although your question looks like 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 if internet connection is available with c #

+4
source

"Connected to the Internet" has no actual meaning, except in the case of a modem. A way to use it to check if a resource is available is to use it. You still have to deal with errors, you do not need to quote everything twice.

+2
source

All Articles