Quantcast
Channel: MSDN Blogs
Viewing all articles
Browse latest Browse all 35736

Windows 8: IsConnectedToInternet?

$
0
0

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.


Viewing all articles
Browse latest Browse all 35736

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>