Windows Phone 7 (Mango) Tutorial - 27 - How to Detect whether the Network is Available?

Sometime we need to know whether Network connectivity is available or not from our application. In Windows Phone 7, we have the required API exposed for the developers to detect the status of the network connection and take respective step based on that.


So how to detect it? In this small tip of the tutorial series, we will learn about the API and we will also see how this can be done using a small demo which includes code too. Read to know more about it.


Index - Windows Phone 7 (Mango) Tutorial


Know about the APIs

Let's discuss about the API first. NetworkInterface class present inside the Microsoft.Phone.Net.NetworkInformation namespace is responsible to inform you whether the network connection is available or not. NetworkInterface class has a method called GetIsNetworkAvailable(). Once called it will return boolean value true or false based on the current network availability.


Microsoft.Phone.Net.NetworkInformation.NetworkInterface class inherits from the base class System.Net.NetworkInformation.NetworkInterface class. Find the complete Meta Data of the classes here:


Meta Data of Microsoft.Phone.Net.NetworkInformation.NetworkInterface


Meta Data of System.Net.NetworkInformation.NetworkInterface


Modifying the Code

It's time to create a small demo application to showcase the same. This will clarify many things. Let's create a new Windows Phone 7 application project. Now inside the MainPage.xaml, we will create a DependencyProperty "NetworkAvailability" of type string which will return the text string displaying whether the network is available or not.


Inside the constructor, we will call the NetworkInterface.GetIsNetworkAvailable() method and populate the property properly. Need assistance? Find the code below for your reference:

using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
namespace NetworkAvailabilityDemo
    public partial class MainPage : PhoneApplicationPage
        public string NetworkAvailability
            get { return (string)GetValue(NetProperty); }
            set { SetValue(NetProperty, value); }
        public static readonly DependencyProperty NetProperty =
                                         new PropertyMetadata(string.Empty));
        // Constructor
        public MainPage()
            NetworkAvailability = NetworkInterface.GetIsNetworkAvailable() ? 
                                                "available" : "not available";


Now move onto the XAML page and add a TextBlock inside the ContentPanel Grid control. Bind the text properly with the DependencyProperty that we exposed. Find the code here in case you need reference:

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <TextBlock Text="{Binding NetworkAvailability, ElementName=phonePage, StringFormat='Internet Connection is {0}'}" FontSize="30"/>


Remember to use the ElementName attribute to the name of the class while binding. We named the XAML page as "phonePage" and hence the ElementName is set to phonePage to retrieve the proper bindings.



Let's see the demo of it. Run the application inside the Phone Emulator. Once the application runs, it will show that the Internet Connection is Available. Here, the API returns True and based on this, our code sets the value in the screen.


Find the screenshot of the running demo here:


WP7.1 Demo - Detecting Network Connectivity


Hope this gave you some basic understandings about the API. Now you will be able to use it in your application to check whether the network connectivity is available to your phone and depending upon the returned value you will be able to take action based on your business requirement. Let me know, if you have any feedback. Enjoy reading my articles. Happy Coding. Cheers.

If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly.

Also, don't forget to share your views and/or feedback in the comment section below.


  1. Hi,

    Did you try to call this method on a slow network? I read many articles that it can take up to 1 minute and to call this method, it should be done in a background thread.


  2. Hi,
    I also read many articles too regarding that this method should be done in a background thread especially if you run into a situation where the network is really slow. Kunal, do you have any thoughts regarding this issue?

  3. Hi,
    I didn't hear about this. I will check if I can get more information on it. BTW, thanks for informing.

  4. From http://msdn.microsoft.com/en-us/library/ff967560%28v=vs.92%29.aspx

    Network Information

    When Windows Phone starts, it can take up to 20 seconds for the first call to the M:System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable to return. This is because Windows Phone initially enumerates all the available network connections. For ways to call the GetIsNetworkAvailable method in the background, see the section Background Threads and Asynchronous Programming later in this topic.

  5. If we talk about the emulator this function will always return true!!
    So do we have any function or method to know the network connectivity in emulator also?

  6. Hello Sir,
    i am developing wp7 app which is totally depend on webservice. I want to add NetworkStateChanged event in my app so that i can detect whenever connectivity losses or gains at any page throughout in my app. How should i achieve this? please guide me.


© 2008-2018 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top