6

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;
                }
            }
        }
Arrabi
  • 3,718
  • 4
  • 26
  • 38
  • I think you will have to check with the enums [NetworkInterfaceType](http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterfacetype%28v=vs.100%29.aspx) when you do `NetworkInterface.GetAllNetworkInterfaces()` – V4Vendetta Nov 29 '12 at 12:12
  • 1
    I did, but it's not really clear about the 3G connection. – Arrabi Nov 29 '12 at 12:42

1 Answers1

4

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.

PhonicUK
  • 13,486
  • 4
  • 43
  • 62
  • it may be an ADSL or dialup connection. what best way to detect that. I think ADSL will be "NetworkInterfaceType.AsymmetricDsl" – Arrabi Nov 29 '12 at 12:31
  • 1
    @Arrabi - A 3G connection will appear as a ADSL or Dialup connection. You are asking for the way to detect this, which is a subjective question, so what have you tried and why are you unhappy with it? – Security Hound Nov 29 '12 at 12:44
  • @Ramhound, I updated my question. sorry for the misunderstanding. – Arrabi Nov 29 '12 at 13:10