[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,515
    7,170
    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,813
    45,775
    150
    #2 CODYQX4, Sep 18, 2012
    Last edited: Apr 12, 2019
    .
     
  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,813
    45,775
    150
    #4 CODYQX4, Sep 22, 2012
    Last edited: Apr 12, 2019
    .
     
  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,515
    7,170
    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...