[C#] - Simply method to detect if the user is connected to internet

Discussion in 'Mixed Languages' started by Josh Cell, Aug 14, 2012.

  1. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,110
    120
    #1 Josh Cell, Aug 14, 2012
    Last edited by a moderator: Apr 20, 2017
    Code:
            public static bool IsConnectedToInternet
            {
                get
                {
                    try
                    {
                        HttpWebRequest hwebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
                        hwebRequest.Timeout = 10000;
                        HttpWebResponse hWebResponse = (HttpWebResponse)hwebRequest.GetResponse();
                        if (hWebResponse.StatusCode == HttpStatusCode.OK)
                        {
                            return true;
                        }
                        else return false;
                    }
                    catch { return false; }
                }
            }
    :schmorch:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  2. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,810
    45,355
    150
    Nice method, and I wanted to do a check as opposed to force disable anyone's connection.

    However, my connection is a laggy, slow, droppy piece of crap, and a hiccup in the connection will cause the check to say I'm not connected.

    My connection will drop, occasionally show the yellow icon indicating "Limited" connectivity. The connection could at any point stabilize, leading to fully working internet in time for the Activation Restore.

    Leaves me to think is there a more reliable check on the adapter state. I don't want to just say "Limited" = "No Internet" as a crap connection may drop into it like in my case.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. master131

    master131 MDL Novice

    Apr 12, 2011
    45
    22
    0
    #3 master131, Sep 22, 2012
    Last edited by a moderator: Apr 20, 2017
    What about this:
    Code:
    NetworkInterface.GetIsNetworkAvailable()
    I tested it and it worked for me. I had 1 network device under Network Connections, and disabled it. That function returned false. I also unplugged my ethernet cable too and got the same result. Then I re-enabled it and re-plugged it and it returned true. I don't know what will happen if your connected is Limited though.

    The only problem with that function is that it doesn't actually check if you can access the internet, just if any network adapter is up.

    A more reliable method would be to use the Network List Manager API available in Windows Vista and 7 to detect if any connection is connected to the internet or whether the state of any adapter/interface is limited.

    You can find a .NET implementation here: http://archive.msdn.microsoft.com/WindowsAPICodePack

    Then, after you've referenced Microsoft.WindowsAPICodePack.dll, you can use NetworkListManager.IsConnectedToInternet to check if the computer is connected to the internet. Once again, I don't know what that property will return if the connected is Limited but you can iterate through each connection manually and determine that yourself if needed.

    Make sure to check if the computer is running at least Windows Vista using CoreHelpers.RunningOnVista which also return true on Windows 7 otherwise it will throw an exception saying the OS isn't supported.
     
  4. CODYQX4

    CODYQX4 MDL Developer

    Sep 4, 2009
    4,810
    45,355
    150
    Googling it shows people having it be very unreliable, and recommending to just check connectivity to an endpoint, which is what the current method does.

    Anyway, I guess there is no way to 100% guarantee the connection down won't come up, and I don't want to disable adapters, so it will just have to do as is.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. master131

    master131 MDL Novice

    Apr 12, 2011
    45
    22
    0
    I suppose so. Also, you might want to look at my post edit if you haven't already.
     
  6. Josh Cell

    Josh Cell MDL Developer

    Jan 8, 2011
    3,519
    7,110
    120
    The "GetIsNetworkAvailable" method really gets the OS connection status and isn't 100% trusted IMO.

    The system can be show an wrong status because haven't updated in the time. For "limited" connections also.

    The "brute" method really open an internet socket and see if have an stable connection. If no catch. If yes the timeout is 10 seconds to capture the response. (limited connection will timeout).
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...