zoukankan      html  css  js  c++  java
  • 获取Mac物理网卡

     public static Dictionary<string, string> GetDefaultAddressInfos()
            {
                Dictionary<string, string> pairs = new Dictionary<string, string>();
                var ip = from nic in NetworkInterface.GetAllNetworkInterfaces()
                         let searchSub = from p in nic.GetIPProperties().UnicastAddresses
                                         where p.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(p.Address)
                                         select p
                         where
                         nic.OperationalStatus == OperationalStatus.Up
                         && (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet || nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                         && searchSub.Any() // && nic.GetIPProperties().GatewayAddresses.Count>0
                         select new { PhysicalAddress = nic.GetPhysicalAddress().ToString(), IPAddress = (searchSub).FirstOrDefault()?.Address.ToString() };
                ip.ToList().ForEach(ipx => pairs.Add(ipx.PhysicalAddress, ipx.IPAddress));
                return pairs;
            }
     public static Dictionary<string, string> GetDefaultAddressInfos()
            {
                Dictionary<string, string> pairs = new Dictionary<string, string>();
                var ip = from nic in NetworkInterface.GetAllNetworkInterfaces()
                         let searchSub = from p in nic.GetIPProperties().UnicastAddresses
                                         where p.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork && !System.Net.IPAddress.IsLoopback(p.Address)
                                         select p
                         where
                         nic.OperationalStatus == OperationalStatus.Up
                         && (nic.NetworkInterfaceType == NetworkInterfaceType.Ethernet || nic.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
                         && searchSub.Any() // && nic.GetIPProperties().GatewayAddresses.Count>0
                         select new { PhysicalAddress = nic.GetPhysicalAddress().ToString(), IPAddress = (searchSub).FirstOrDefault()?.Address.ToString() };
                ip.ToList().ForEach(ipx => pairs.Add(ipx.PhysicalAddress, ipx.IPAddress));
                return pairs;
            }
    
    
    
      private static string GetMacAddressByNetworkInformation()
            {
                string key = "SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\";
                string macAddress = string.Empty;
                try
                {
                    NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                    foreach (NetworkInterface adapter in nics)
                    {
                        if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet
                            && adapter.GetPhysicalAddress().ToString().Length != 0)
                        {
                            string fRegistryKey = key + 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" || fPnpInstanceID.Contains("PCI")))
                                {
                                    macAddress = adapter.GetPhysicalAddress().ToString();
    
    ​                                break;
    ​                            }
    ​                        }
    ​                    }
    ​                }
    ​            }
    ​            catch (Exception)
    ​            {
    ​                //这里写异常的处理
    ​            }
    ​            return macAddress;
    ​        } 
  • 相关阅读:
    Windows 窗体设计器中的设计时错误
    union all 里面的order by
    docx转doc时,防止公式被转成图片的解决办法
    学习方向推荐
    关于验收测试的几个困惑
    《实例化需求》读书笔记
    VS2010中使用 SpecFlow + Selenium.WebDriver
    敏捷团队成员应具备的素质
    Jolt Awards: The Best Books
    在Ajax.ActionLink的OnBegin,onComplete等事件中使用this【解决办法】
  • 原文地址:https://www.cnblogs.com/ingstyle/p/12886373.html
Copyright © 2011-2022 走看看