获得网卡物理地址

2015-10-10 09:23:32  访问(1873) 赞(0) 踩(0)


        /// <summary>
        /// 获得网卡物理地址
        /// </summary>
        /// <returns></returns>
        public string NetworkAddressGet()
        {
            return GetNetworkAddress(true);
        }


        /// <summary>
        /// 获取主板物理网卡地址
        /// </summary>
        /// <returns></returns>
        private string GetNetworkAddress(bool isValid)
        {
            try
            {
                StringBuilder theResult = new StringBuilder();
                bool isFirst = true;

                string strMac = null;
                NetworkInterface[] fNetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in fNetworkInterfaces)
                {
                    strMac = null;

                    if (isValid)
                    {

                        string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + adapter.Id + "\\Connection";
                        RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
                        if (rk != null)
                        {
                            string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
                            //int fMediaSubType = Convert.ToInt32(rk.GetValue("MediaSubType", 0));
                            if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
                            {
                                if (adapter.GetPhysicalAddress() != null)
                                {
                                    strMac = adapter.GetPhysicalAddress().ToString();
                                }
                            }
                            //else
                            //{
                            //    if (fMediaSubType == 2)
                            //    {
                            //        //无线网卡
                            //    }
                            //    if (fMediaSubType == 1)
                            //    {
                            //        //虚拟网卡
                            //    }
                            //}
                        }
                    }
                    else
                    {
                        if (adapter.GetPhysicalAddress() != null)
                        {
                            strMac = adapter.GetPhysicalAddress().ToString();
                        }
                    }

                    if (strMac != null && strMac.Length > 0)
                    {
                        if (isFirst)
                        {
                            isFirst = false;
                        }
                        else
                        {
                            theResult.Append("|");
                        }

                        theResult.Append(strMac);
                    }
                }

                return theResult.ToString();

            }
            catch
            {
                return "";
            }
        }


标签:C#    WinForm    网卡    物理地址    mac地址 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)
 
  ┈全部┈  
 
(显示默认分类)