简体   繁体   中英

What is the best way to check internet connection WITHOUT accessing the internet in C#?

I know there are many ways of doing it, I've read the SO posts.

I do not want to access a website, or externally if there is internet.

Is there a way to check internet status by checking the network card? In other words, when the laptop shows internet bars etc or computer itself shows "connected".

This method causes my code to be stuck:

private bool checkInternetStatus()
{
    try
    {
        using (var client = new WebClient())
            using (client.OpenRead("http://google.com/generate_204")) 
                online = true;
            //  Print("Connection estabished");
                return true; 
    }
    catch
    {
        online = false;
        Print("No internet connection");
        return false;
    }
}

You could use Ping, programmatically, in C#. No web client needed.

I think this post shows a few different answers to your question.

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