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
6 Comments
Good stuff, thanks for the post.
Thanks John
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
Thanks for the good post,
I want to disable the wifi network connection in C#.
How can I do that ??
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.