How to get network information for Windows Phone 8
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
When you create a Windows Phone app, you might want to get network information for the user’s phone. You can do this by using the properties of the DeviceNetworkInformation class. Because the properties are static, you do not have to create an instance of the class first; you can just access the properties directly. You can use the following properties:
Note
You can also determine the mobile operator of a user’s phone. For more information, see How to determine the mobile operator for Windows Phone 8.
Getting Network Information
In the following procedure, you put the code in a button click event for testing purposes only. In your own applications, you can access the properties wherever you need them. The following procedure assumes that you have a Windows Phone application that has a page with a button named button1.
To get network information
At the top of the code-behind file for your page, add the following statement.
using Microsoft.Phone.Net.NetworkInformation;
Imports Microsoft.Phone.Net.NetworkInformation
Add the following code to your button click event.
private void button1_Click(object sender, RoutedEventArgs e) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("Network available: "); sb.AppendLine(DeviceNetworkInformation.IsNetworkAvailable.ToString()); sb.Append("Cellular enabled: "); sb.AppendLine(DeviceNetworkInformation.IsCellularDataEnabled.ToString()); sb.Append("Roaming enabled: "); sb.AppendLine(DeviceNetworkInformation.IsCellularDataRoamingEnabled.ToString()); sb.Append("Wi-Fi enabled: "); sb.AppendLine(DeviceNetworkInformation.IsWiFiEnabled.ToString()); MessageBox.Show(sb.ToString()); }
Private Sub Button1_Click(sender As System.Object , e As System.Windows.RoutedEventArgs) Handles Button1.Click Dim sb As new System.Text.StringBuilder() sb.Append("Network available: ") sb.AppendLine(DeviceNetworkInformation.IsNetworkAvailable.ToString()) sb.Append("Cellular enabled: ") sb.AppendLine(DeviceNetworkInformation.IsCellularDataEnabled.ToString()) sb.Append("Roaming enabled: ") sb.AppendLine(DeviceNetworkInformation.IsCellularDataRoamingEnabled.ToString()) sb.Append("Wi-Fi enabled: ") sb.AppendLine(DeviceNetworkInformation.IsWiFiEnabled.ToString()) MessageBox.Show(sb.ToString()) End Sub
Save and build your solution.
Start your application and click the button.
The message box should appear and your output should look like the following:
Network available: True
Cellular enabled: False
Roaming enabled: False
Wi-Fi enabled: False
Note
Your output will be different depending on the capabilities of your network.
See Also
Reference
Other Resources
How to determine the mobile operator for Windows Phone 8
Network and network interface information for Windows Phone 8