获取系统当前网络的类型

来源:互联网 发布:台风海燕 知乎 编辑:程序博客网 时间:2024/05/22 21:53

/// <summary>
        /// 获得所有工作状态的网络适配器名称
        /// </summary>
        /// <returns></returns>
        public static NetworkInterface[] GetNetCardAdapters()
        {
            List<NetworkInterface> netCardAdapters = new List<NetworkInterface>();
             NetworkInterface[] netWorks = NetworkInterface.GetAllNetworkInterfaces();
             foreach (NetworkInterface adapter in netWorks)
             {
                 if (adapter.NetworkInterfaceType == NetworkInterfaceType.Tunnel)
                     continue;


                 string mac = adapter.GetPhysicalAddress().ToString();


                 if (string.IsNullOrEmpty(mac) || adapter.OperationalStatus != OperationalStatus.Up)
                 {
                     continue;
                 }


                 int adapterType = IdentifyAdapterType(adapter.Id);
                 //去除虚拟网卡与未知设备
                 if (adapterType < 2)
                 {
                     continue;
                 }
                 netCardAdapters.Add(adapter);
             }
             return netCardAdapters.ToArray();
        }


        /// <summary>
        /// 分辨网卡是真实,虚拟,还是无线
        /// </summary>
        /// <param name="adapterId"></param>
        /// <returns>0未知网卡,1虚拟,2无线,3真实网卡</returns>
        private static int IdentifyAdapterType(string adapterId)
        {
            string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapterId + "\\Connection";


            RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
            if (rk != null)
            {
                // 区分 PnpInstanceID    
                // 如果前面有 PCI 就是本机的真实网卡   
                // MediaSubType 为 01 则是常见网卡,02为无线网卡。   
                string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
                int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
                if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
                    return 3;
                else if (fMediaSubType == 1)
                    return 1;
                else if (fMediaSubType == 2)
                    return 2;
                else if (fMediaSubType == 9)//3G网卡的值
                    return 9;
            }
            return 0;
        }


        /// <summary>
        /// 获取当前网络连接类型
        /// </summary>
        /// <param name="ip"></param>
        public static NetWorkType GetNetWorkType()
        {
            NetWorkType netType = NetWorkType.UnKnow;
            NetworkInterface[] netCards = GetNetCardAdapters();
            foreach (NetworkInterface adapter in netCards)
            {
                if (adapter.Description.Contains("Wireless") && netType != NetWorkType.Wan)
                {
                    netType = NetWorkType.WLan;
                }
                else if (adapter.Description.Contains("3G"))
                {
                    if (netType == NetWorkType.UnKnow)
                    {
                        netType = NetWorkType.W3G;
                    }
                }
                else
                {
                    netType = NetWorkType.Wan;
                }
            }
            return netType;
        }

原创粉丝点击