2

I'm working on a "regular" C# WPF application (no UWP app), and I need to know whether I'm on a cellular connection. Cellular connection being both when using a simcard inside the device, or when using a hotspot from a mobile phone.

How can I achieve this?

Background: Since the application might download lots of data, I want the users to have a choice whether they'd like to avoid downloading on cellular connections or not)

Other SO questions: I've seen this question and this question, they're focusing on UWP functionality. Also this one, which is just focusing on the connection itself, rather then whether that's a cellular connection or not.

Peter van Kekem
  • 1,387
  • 1
  • 12
  • 30

2 Answers2

2

I experimented with the UWP libraries and was able to solve it using UWP references.

I've added this reference:

C:\Program Files (x86)\Windows Kits\10\References\10.0.17763.0\Windows.Foundation.UniversalApiContract\7.0.0.0\Windows.Foundation.UniversalApiContract.winmd

And used this code to check for metered connections:

private void CheckIsMetered()
{
    var profile = Windows.Networking.Connectivity.NetworkInformation.GetInternetConnectionProfile();
    IsInternetAvailable = profile != null && profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.InternetAccess;
    if (IsInternetAvailable)
        IsMetered = profile.GetConnectionCost().NetworkCostType != Windows.Networking.Connectivity.NetworkCostType.Unrestricted;
}

I use the NetworkStatusChanged event to re-check the IsMetered.

CheckIsMetered();
NetworkInformation.NetworkStatusChanged += (s) => CheckIsMetered(); 

This works in my WPF app.

Peter van Kekem
  • 1,387
  • 1
  • 12
  • 30
1

You can identify your machine network adapters with System.Net.NetworkInformation.

Furthermore, you can take advantage of the property NetworkInterface.OperationalStatus to filter the adapters that are currently connected and NetworkInterfaceType to identify the type of connection. Check the example below.

Unfortunately I don't have a cellular modem on my machine to check if it returns the desired result, but try it and let us know if it works.

 NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
 foreach (NetworkInterface adapter in interfaces)
    {
        //Check if it's connected
        if (adapter.OperationalStatus == OperationalStatus.Up
            //The network interface uses a mobile broadband interface for WiMax devices.
            && (adapter.NetworkInterfaceType == NetworkInterfaceType.Wman
                //The network interface uses a mobile broadband interface for GSM-based devices.
                || adapter.NetworkInterfaceType == NetworkInterfaceType.Wwanpp
                //The network interface uses a mobile broadband interface for CDMA-based devices.
                || adapter.NetworkInterfaceType == NetworkInterfaceType.Wwanpp2))
        {
            //adapter probably is cellular
        }                
    }
kyriakosio
  • 106
  • 6
  • Thank you for your answer, this is absolutely helpful. However, it doesn't detect when I'm using my mobile phone as a hotspot (where for instance Onedrive immediately gives a pop-up saying syncing is suspended due to cellular connection. – Peter van Kekem Mar 13 '19 at 19:10