Many applications utilize network connection for various purposes like feeds, sending & receiving data to/from server, etc. The following article explains how one can detect if the network connection is available or not.
Network API Information
- NetworkInterface Class helps in providing network information to the developers. It helps detect if the network is on/off, address etc.
- NetworkInterface::GetIsNetworkAvailable is used to check if the network connection is available.The network connection is considered to be available if interface is not in loopback or is marked as “upâ€.
- Namespace: System.Net.NetworkInformation
Example Code to understand the network connection
namespace NetworkTest
{
public partial class MainPage : PhoneApplicationPage
{
public static readonly DependencyProperty NetProperty =
DependencyProperty.Register("NetworkAvailability",
typeof(string),
typeof(MainPage),
new PropertyMetadata(string.Empty));
public MainPage()
{
InitializeComponent();
string isNetworkAvail =
NetworkInterface.GetIsNetworkAvailable() ? "on" : "off";
}
}
}