简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM