• References
  • Contact
    • CampusNet for Windows 8 Privacy Policy
    • Hvad har åbent nu? Privatlisbeskyttelse
    • Privacy Policy for Qua apps
Kasper Holdum C#, Windows Phone, Windows 8 and much more

Checking for Connectivity the Bulletproof Way

October 12, 2012 16:11 / 3 Comments / Kasper Holdum
  • Facebook
  • Twitter
  • Email

A huge amount of the currently released apps in the Store does a check upon app start up to see if connectivity is available. If this is not the case, then the user is redirected to an offline page, and there is no way to continue use of the app.

This is a major concern, when several of the apps does not correctly check for connectivity. One such case, is when the user connects to a VPN. In that case the main connection will be changed to ‘Limited’ even though the user still has an active connection to the internet.

The below snippet will check all the connection profiles for internet access:

C#
1
2
3
4
5
6
7
8
9
10
11
public static bool IsConnected
{
    get
    {
        var profiles = NetworkInformation.GetConnectionProfiles();
        var internetProfile = NetworkInformation.GetInternetConnectionProfile();
        return profiles.Any(s => s.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess)
           || (internetProfile != null
                   && internetProfile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
    }
}

The naive way to check for internet access would be:  NetworkInformation.GetInternetConnectionProfile().GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess , but this approach fails in the scenario described above.

Posted in: windows8 / Tagged: connectivity, internetaccess

3 comments on “Checking for Connectivity the Bulletproof Way”

  1. Michiel Post on October 22, 2012 at 21:59 said:

    Just a little warning, the method described here is NOT bulletproof.

    internetProfile can be NULL, in that case this code throws an exception. A null check on internetProfile should be added.

    Reply ↓
    • Kasper Holdum on October 24, 2012 at 02:37 said:

      Ahh, you are absolutely correct. I initially had this check, but somehow it got left out when I updated the post.

      Reply ↓
  2. BestW8Apps.com on November 8, 2012 at 00:31 said:

    Thanks for sharing! I had issues with the Internet connection and I only suspected it’s because of Ethernet connection. I will try your solution and I am almost sure it will help me! )

    –
    Roman Empire

    Reply ↓

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

*

*

HTML tags are not allowed.

Post Navigation

← Previous Post
Next Post →

About

Computer science bachelor with a passion for .NET / C#. In the process of finishing a masters in computer science. Works part time as an ASP.NET developer where I specialize in web architecture using Entity Framework Code First and MVC.

On the side I freelance on Windows Phone / Windows 8 projects.

For more information check the reference and contact pages.

Recent Posts

  • Throttling Search Queries On TextChanged With Cancellation Support
  • DDC 2013 Slides and Demo Project
  • Danish Developer Conference 2013
  • Using GridView/ListView’s Built-in Animations
  • Slides/Demos from ‘Async in C#’ CampusDays2012 Presentation

Archives

  • April 2013
  • February 2013
  • November 2012
  • October 2012

Categories

  • c#
  • social
  • Uncategorized
  • windows8
© Copyright 2013 - Qua
Infinity Theme by DesignCoral / WordPress