简体   繁体   English

有没有办法检测我正在使用什么样的连接? WiFi,3G还是以太网?

[英]Is there a way to detect what kind of connection I'm using ? WiFi, 3G or Ethernet?

I'm trying to detect what kind of network connection I'm connected to. 我正在尝试检测我连接的网络连接类型。 is it WiFi or 3G? 是WiFi还是3G? is there a way to do that using c# win forms .net 2.0 or 4.0 ? 有没有办法使用c#win forms .net 2.0或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;
                }
            }
        }

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). 3G连接看起来与ADSL或拨号连接相同(网络类型为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. 如果您确定自己只使用WiFi / 3G,则可以检查GetAllNetworkInterfaces提供的NetworkInterface类中的信息,如果接口类型为PPP, 则将其视为3G。 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. 编辑:你可能有运气在设备名称或描述中寻找“3G”,“HSPA”,“HSDPA”,“Dongle”。 But this'd only be a 'decent guess' rather than being absolutely certain. 但这只是一个“体面的猜测”,而不是绝对肯定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM