Share via


Windows 8: IsConnectedToInternet?

In writing my own app, I found myself wondering, “how to I detect if the machine is actually connected to the internet before I start making all manner of web services calls?”. Well, below is my solution written in C#. Enjoy!

 

Code:

 private bool IsConnectedToInternet()
{
    bool connected = false;

    ConnectionProfile cp = NetworkInformation.GetInternetConnectionProfile();

    if (cp != null)
    {
        NetworkConnectivityLevel cl = cp.GetNetworkConnectivityLevel();

        connected = cl == NetworkConnectivityLevel.InternetAccess;
    }

    return connected;
}

 

Usage:

 if(!IsConnectedToInternet())
{
     //some kind of error handling here
}
else
{
     //we're good to go, so do something with it!
}

If you find a better way of doing it, please let me know in the comments.

Comments