-->

Is there a way to detect what kind of connection I

2020-07-10 08:37发布

问题:

I'm trying to detect what kind of network connection I'm connected to. is it WiFi or 3G? is there a way to do that using c# win forms .net 2.0 or 4.0 ?

        foreach (NetworkInterface adapter in adapters)
        {
            if (adapter.OperationalStatus == OperationalStatus.Up)
            {
                if (adapter.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                {
                    lblNetworkType.Text = "you are using WiFi";
                    break;
                }
                else if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ppp)
                {
                    lblNetworkType.Text = "you are using 3G or ADSL or Dialup";
                    break;
                }
                else if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
                {
                    lblNetworkType.Text = "you are using Ethernet";
                    break;
                }
            }
        }

回答1:

Unfortunately there isn't a 'neat' way to do this as such. A 3G connection will look the same as a ADSL or dialup connection (with the network type being PPP).

If you are certain that you'll only ever be on WiFi/3G, then you could check the information in the NetworkInterface class provided by GetAllNetworkInterfaces and treat it as 3G if the the interface type is PPP. But as I mentioned this is the same for other types of modem connection.

Edit: You may have some luck looking for "3G", "HSPA", "HSDPA", "Dongle" in the device name or description. But this'd only be a 'decent guess' rather than being absolutely certain.