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.
Method to use to check if internet connection is available or not:
- public void checkinternet()
- {
- var connection = NetworkInformation.GetInternetConnectionProfile();
- bool status = (connection != null &&
- connection.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess);
- }
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.
- NetworkInformation.NetworkStatusChanged += NetworkInformation_NetworkStatusChanged;
- void NetworkInformation_NetworkStatusChanged(object sender)
- {
- if (DeviceNetworkInformation.IsNetworkAvailable)
- {
-
- }
- }
Next, using this NetworkInformation class we can find the type of network we are using,
Code to find the type of network we are using in Windows 10 UWP App.
- var currentNetworkConnection = NetworkInformation.GetInternetConnectionProfile();
- var currenttype = currentNetworkConnection.NetworkAdapter.IanaInterfaceType;