Detecting an Internet Connection in Windows 8

I couldn’t find any simple sample code online about detecting an internet connection in Windows 8 so here it is! Windows 8 Store Apps will fail store certification if they don’t check for a internet connection before trying to use one.

public static string CheckForInternetConnection()
{             
    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

    if (InternetConnectionProfile != null)
    {
        switch (InternetConnectionProfile.GetNetworkConnectivityLevel())
        {
            case NetworkConnectivityLevel.None:
                return "No Internet Connection Detected.";
            case NetworkConnectivityLevel.LocalAccess:
                return "Only Local Access, No Internet Connection Detected.";
            case NetworkConnectivityLevel.ConstrainedInternetAccess:
                return "Limited Internet Connection Detected.";
            case NetworkConnectivityLevel.InternetAccess:
                return "Internet Connection Detected.";
            default:
                return "No Internet Connection Detected.";
        }
    }
    else
    {
        return "No Internet Connection Detected.";
    }
}

Just be sure to include the Windows.Networking.Connectivity namespace.

using Windows.Networking.Connectivity;

Source: MSDN Network information sample

Advertisements

Published by

Alan Feekery

Developer, Gamer, Musician, Cyclist and big Motorsport fan... enjoys the odd cup of coffee :)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s