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; } } }
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.
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).