How to find if the Network is Available in WP7 using C#

How do one check if there is a open network connection in the Windows Phone 7 using c# ? .

You can achieve it through the static method GetIsNetworkAvailable() that is part of the NetworkInterface class .

You might require to include the namespace NetworkInformation incase you want to use this method.

using System.Net.NetworkInformation;

bool isAvailable = NetworkInterface.GetIsNetworkAvailable();

MessageBox.Show(isAvailable.ToString());

Note that when you use this method in the emulator , it will return always true , but in the device it should return the actual value .

The developers can also use the NetworkInterfaceType defined in the class “Microsoft.Phone.Net.NetworkInformation.NetworkInterface” property to detect the type of network connection .

MessageBox.Show(Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType.ToString());

The list of the Network Connection types are defined in the namespace “Microsoft.Phone.Net.NetworkInformation” and the
Microsoft.Phone.dll file .

Note that this will display Wireless80211 when i tried it in the emulator .

To know about the different connection types , you can find it on NetworkInterfaceType Enumeration

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

9 Responses

  1. Pingback: How to find if the Network is Available in WP7 using C#
  2. Microsoft.Phone.Net.NetworkInformation.NetworkInterface.NetworkInterfaceType.ToString()

    will return “None” if you r not connect to network.
    and will give you value “Wireless80211” if you r connecty to network irrespective of any network you are connected

  3. Hi,
    Following is my code:
    if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
    {
    MessageBox.Show(“Network Unavailable! This application cannot continue without network availability.”);
    }

    But i’m having the following run-time exception (it compiles fine):

    The type ‘System.Net.NetworkInformation.NetworkInterface’ exists in both ‘System.dll’ and ‘System.Net.dll’

    Please help me in this regard ASAP. Thanks a lot in advance.

Leave a Reply