C#获得本机物理网卡的MAC地址(备忘)

因项目需求,需要自动收集网卡的相关信息,在网上找了一大堆,但是完全符合要求的不多,有些看似符合要求,但是运行出来的结果,和真实情况有所偏差,所以根据真实环境,在网上搜来的代码上进行了一些修改,不一定完全正确,需要后期多试几台机器,先这样用着。

/// <summary>
        /// 取网卡信息
        /// </summary>
        /// <returns></returns>
        public static string GetNetAdapterInfo()
        {
            StringBuilder sb = new StringBuilder();
            NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
            if (adapters != null)
            {
                foreach (NetworkInterface ni in adapters)
                {
                    string fCardType = "未知网卡";
                    IPInterfaceProperties ips = ni.GetIPProperties();
                    
                    PhysicalAddress pa = ni.GetPhysicalAddress();
                    if (pa == null) continue;
                    string pastr = pa.ToString();
                    if (pastr.Length < 7) continue;
                    if (pastr.Substring(0, 6) == "000000") continue;
                    if (ni.Name.ToLower().IndexOf("vmware") > -1) continue;
                    string fRegistryKey = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\" + ni.Id + "\\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")
                         if (ni.NetworkInterfaceType.ToString().ToLower().IndexOf("wireless")==-1)
                             fCardType = "物理网卡";
                         else
                             fCardType = "无线网卡";
                     else if (fMediaSubType == 1 || fMediaSubType == 0)
                         fCardType = "虚拟网卡";
                     else if (fMediaSubType == 2 || ni.NetworkInterfaceType.ToString().ToLower().IndexOf("wireless")>-1)
                         fCardType = "无线网卡";
                     else if (fMediaSubType == 7)
                         fCardType = "蓝牙";
                    }
                    StringBuilder isb = new StringBuilder();
                    UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = ips.UnicastAddresses;    
                     foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)    
                     {    
                         if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)    
                             isb.Append(string.Format("Ip Address: {0}", UnicastIPAddressInformation.Address)+"\r\n"); // Ip 地址    
                     }

                    // IPAddressCollection ipc = ips.DnsAddresses;
                    //if (ipc.Count > 0)
                    //{
                    //    foreach (IPAddress ip in ipc)
                    //    {
                    //        isb.Append(string.Format("DNS服务器地址:{0}\r\n",ip));
                    //    }
                    //}
                    string s = string.Format("{0}\r\n描述信息:{1}\r\n类型:{2}\r\n速度:{3} MB\r\nMac地址:{4}\r\n{5}",fCardType,ni.Name,ni.NetworkInterfaceType,ni.Speed/1024/1024,ni.GetPhysicalAddress(),isb.ToString());
                    sb.Append(s+"\r\n");
                }
            }
            return sb.ToString();
        }


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值