31

I would like to check internet connectivity type in Windows Universal Application.

  1. Not Connected
  2. Connected via WLAN(WiFi)
  3. Connected via WWAN(Cellular Data)
  4. Connected to a metered network

in order to provide an option for downloading large size content. And also sense the significant network availability changes.

Currently, I'm only able to check whether internet connected or not using GetIsNetworkAvailable method of NetworkInterface class.

NetworkInterface.GetIsNetworkAvailable();
Vineet Choudhary
  • 7,433
  • 4
  • 45
  • 72
  • 1
    Possible duplicate of [Windows 10 UWP - detect if the current internet connection is Wifi or Cellular?](http://stackoverflow.com/questions/32845625/windows-10-uwp-detect-if-the-current-internet-connection-is-wifi-or-cellular) – Igor Ralic Mar 09 '16 at 20:35
  • @igrali http://stackoverflow.com/questions/32845625/windows-10-uwp-detect-if-the-current-internet-connection-is-wifi-or-cellular answer of this question only cover the WLAN and WWAN answer. In my question three more part included 1. Not Connected, 2. Metered Network (Required because of large size content) and 3. Sense of significant network availability changes. So,you can say 40% duplicate :P – Vineet Choudhary Mar 09 '16 at 20:57
  • Check out the other answers, too, not just the accepted answer. – Igor Ralic Mar 09 '16 at 23:49
  • @igrali Including other answer which not anywhere mention in question, still my question 60% duplicate :P – Vineet Choudhary Mar 10 '16 at 04:22
  • Bear in mind that network connectivity can change at any time and it's not under the control of your software. This means that between discovering the "answer" and actually making use of it, the reality of the situation may have changed. – Damien_The_Unbeliever Mar 10 '16 at 07:37
  • @Damien_The_Unbeliever i just want to notify my app when network availability change not change through app. BTW I found the answer https://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj835820.aspx – Vineet Choudhary Mar 10 '16 at 07:45

3 Answers3

57

1. Check Internet Connection Availability

To check whether any network connection is established or not use GetIsNetworkAvailable method of NetworkInterface class.

bool isNetworkConnected = NetworkInterface.GetIsNetworkAvailable();

GetIsNetworkAvailable() -
Summary: Indicates whether any network connection is available.
Returns: true if a network connection is available; otherwise, false.


2. Check Internet Connection Availability via WWLN (WiFi)

To check whether internet connected via WWAN use IsWlanConnectionProfile property of ConnectionProfile class

ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
bool isWLANConnection = (InternetConnectionProfile == null)?false:InternetConnectionProfile.IsWlanConnectionProfile;

IsWlanConnectionProfile
Summary: Gets a value that indicates if connection profile is a WLAN (WiFi) connection. This determines whether or not WlanConnectionProfileDetails is null.
Returns: Indicates if the connection profile represents a WLAN (WiFi) connection.


3. Check Internet Connection Availability via WWAN (Mobile)

To check whether internet connected via WWAN use IsWwanConnectionProfile property ofConnectionProfile class

ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();
bool isWLANConnection = (InternetConnectionProfile == null)?false:InternetConnectionProfile.IsWwanConnectionProfile;

IsWwanConnectionProfile
Summary: Gets a value that indicates if connection profile is a WWAN (mobile) connection. This determines whether or not WwanConnectionProfileDetails is null.
Returns: Indicates if the connection profile represents a WWAN (mobile) connection.

Reference
Hippiehunter Answer


4. Check Metered network

To check whether Internet reachable via a metered connection or not, use GetConnectionCost method on NetworkInterface class.

var connectionCost = NetworkInformation.GetInternetConnectionProfile().GetConnectionCost();
if (connectionCost.NetworkCostType == NetworkCostType.Unknown 
        || connectionCost.NetworkCostType == NetworkCostType.Unrestricted)
{
    //Connection cost is unknown/unrestricted
}
else
{
   //Metered Network
}

Reference (More detailed answer here)
1. How to manage metered network cost constraints - MSDN
2. NetworkCostType Enum - MSDN


5. Manage network availability changes

To sense the significant network availability changes, use eventNetworkStatusChanged of NetworkInformation class

// register for network status change notifications
 networkStatusCallback = new NetworkStatusChangedEventHandler(OnNetworkStatusChange);
 if (!registeredNetworkStatusNotif)
 {
     NetworkInformation.NetworkStatusChanged += networkStatusCallback;
     registeredNetworkStatusNotif = true;
 }

async void OnNetworkStatusChange(object sender)
{
    // get the ConnectionProfile that is currently used to connect to the Internet                
    ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

    if (InternetConnectionProfile == null)
    {
        await _cd.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            rootPage.NotifyUser("Not connected to Internet\n", NotifyType.StatusMessage);
        });
    }
    else
    {
        connectionProfileInfo = GetConnectionProfile(InternetConnectionProfile);
        await _cd.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            rootPage.NotifyUser(connectionProfileInfo, NotifyType.StatusMessage);
        });
    }
    internetProfileInfo = "";
}

References
Check Internet Connectivity - developerinsider.co

How to manage network connection events and changes in availability - MSDN

How to retrieve network connection information- MSDN


Hope it helpful to someone.

jalsh
  • 801
  • 6
  • 18
Vineet Choudhary
  • 7,433
  • 4
  • 45
  • 72
6

I use NetworkInformation.GetInternetConnectionProfile().IsWlanConnectionProfile and IsWwanConnectionProfile. If neither is true, it should mean you're on Ethernet or something like that.

Keep in mind thatGetInternetConnectionProfile() can return null and can falsely return that there is an active internet connection when the connection is active but DHCP has failed.

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
Hippiehunter
  • 967
  • 5
  • 11
1

To find if the user has any network connection whatsoever (including one without internet) I use

public bool ConnectedToNetwork()
{
    return NetworkInformation.GetInternetConnectionProfile()?.NetworkAdapter != null;
}
dwb
  • 2,136
  • 13
  • 27
  • I rate using this method over Network.GetIsNetworkAvailable() because it's way less CPU intensive. Especially if your using within a loop. – ShaneOss Feb 25 '21 at 10:05