Verify Internet Access And Notify Network Changes In Windows 10 Universal App

In this article we are going to see how to access internet details in Windows 10 universal app.

In internet communication related apps we need to know if the user has an internet connection or not. So we need to check before communication with any services.

For that Windows 10 has Windows.Networking.Connectivity.NetworkInformation class. With this class you can easily get information whether you have internet access or not.

Method to use to check if internet connection is available or not:
  1. public void checkinternet()  
  2. {  
  3.     var connection = NetworkInformation.GetInternetConnectionProfile();  
  4.     bool status = (connection != null &&  
  5.         connection.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);  
  6. }  
Use this method everywhere when you need to check for internet. In some cases, we need to do some task during the network changes. For that we need to register the process for determining the network connectivity by registering a handler for the NetworkStatusChanged event on the NetworkInformation class.

NetworkStatusChanged and when the status changes, get the notification of and store it so it can easily be returned when someone wants to check the internet access.

Code to detect the network changes in Windows 10 universal app.
  1. NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;  
  2. void NetworkInformation_NetworkStatusChanged(object sender)  
  3. {  
  4.     if (DeviceNetworkInformation.IsNetworkAvailable)   
  5.     {  
  6.         //do you task  
  7.     }  
  8. }  
Next, using this NetworkInformation class we can find the type of network we are using,
  • Ethernet
  • WIFI
  • MOBILE
  • NONE
Code to find the type of network we are using in Windows 10 UWP App.
  1. var currentNetworkConnection = NetworkInformation.GetInternetConnectionProfile();  
  2. var currenttype = currentNetworkConnection.NetworkAdapter.IanaInterfaceType;