如何检索计算机的板载以太网MAC地址?

如何检索计算机的板载以太网MAC地址?

问题描述:

我正在尝试从计算机检索板载以太网适配器的MAC地址,以便为设备生成唯一标识符。以下是我正在使用的方法。如何检索计算机的板载以太网MAC地址?

NetworkInterface[] ifConfig = NetworkInterface.GetAllNetworkInterfaces(); 
int maxHash = int.MinValue; 
Guid D = Guid.Empty; 

foreach (NetworkInterface net in ifConfig) 
{ 
    if (net.NetworkInterfaceType == NetworkInterfaceType.Ethernet) 
    { 
       if (maxHash < net.GetPhysicalAddress().ToString().GetHashCode()) 
       { 
        maxHash = net.GetPhysicalAddress().ToString().GetHashCode(); 

        ID = new Guid(String.Concat("00000000-0000-0000-0000-", net.GetPhysicalAddress().ToString())); 

       } 
    } 
} 

然而,蓝牙适配器,适配器虚拟机和其他几个网络适配器也是NetworkInterfaceType.Ethernet类型。我怎样才能获得板载以太网连接的MAC地址?

做一个包含忽略这些是不可能的。任何帮助将非常感激。

+2

这是一个坏主意。主要是因为MAC地址不是唯一的。如果考虑(克隆)虚拟机,情况会变得更糟。请参阅https://superuser.com/a/968346 –

+0

@SteffenWinkler,它仍然是事实上的行业标准方法,正如FlexLM所使用的那样。有什么选择? –

+0

@HarryJohnston正如我所说的你可以生成一个Guid并使用它。你可以做客户,因为在现实中碰撞的机会是不存在的。你不能使用任何硬件来识别计算机。其中一个原因是虚拟机,另一个是对计算机的修改,如交换网卡或添加GPU。更改(缺陷)硬件将改变该计算机的ID。 –

作为选项(不是最好的,但仍然=)) - 您可以尝试使用 度量标准。在大多数情况下,网络到物理网络的度量值 卡优先级

  using System; 
      using System.Collections.Generic; 
      using System.ComponentModel; 
      using System.Data; 
      using System.Drawing; 
      using System.Linq; 
      using System.Management; 
      using System.Net.NetworkInformation; 
      using System.Text; 
      using System.Threading.Tasks; 
      using System.Windows.Forms; 

private void button1_Click(object sender, EventArgs e) 
     { 
      listBox1.Items.Clear(); 
       ManagementObjectSearcher query = new 
       ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'"); 
       ManagementObjectCollection queryCollection = query.Get(); 
       foreach (ManagementObject mo in queryCollection) 
       { 
        if (!(mo["Description"].ToString().Contains("VM"))) 
        { 
         if (!(mo["Description"].ToString().Contains("Virtual"))) 
         { 
          if (!(mo["Description"].ToString().Contains("Hyper"))) 
          { 
           string[] addresses = (string[])mo["IPAddress"]; 

           string IPConnectionMetric = Convert.ToString(mo["IPConnectionMetric"]).Trim(); 

           foreach (string ipaddress in addresses) 
           { 

            listBox1.Items.Add(ipaddress + ";" + IPConnectionMetric); 
           } 

          } 

private void button2_Click(object sender, EventArgs e) 

{ 

    if (listBox1.Items.Count > 1) 
       {  
        int maximum = int.MinValue; 
        int minimum = int.MaxValue; 
        for (int i = 0; i < listBox1.Items.Count; i++) 

        { 
         int output = Convert.ToInt32(listBox1.Items[i].ToString().Split(';')[1]); 
         if ((int)output > maximum) 
          maximum = (int)output; 

        } 

        for (int i = 0; i < listBox1.Items.Count; i++) 
        { 
         int output = Convert.ToInt32(listBox1.Items[i].ToString().Split(';')[1]); 
         if ((int)output < maximum) 
          minimum = (int)output; 

         if (listBox1.Items[i].ToString().Contains(minimum.ToString())) 
         {  
          var minmetric = listBox1.Items[i].ToString(); 

          NetworkInterface[] ifConfig = NetworkInterface.GetAllNetworkInterfaces(); 
          int maxHash = int.MinValue; 
          Guid D = Guid.Empty; 
          foreach (NetworkInterface net in ifConfig) 
          { 
           if (net.NetworkInterfaceType == NetworkInterfaceType.Ethernet) 
           { 
            if (maxHash < net.GetPhysicalAddress().ToString().GetHashCode()) 
            { 
             maxHash = net.GetPhysicalAddress().ToString().GetHashCode(); 

             foreach (UnicastIPAddressInformation ip in net.GetIPProperties().UnicastAddresses) 
             { 

              if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 
              { 

               if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 
               { 
                if (ip.Address.ToString().Contains(minmetric.ToString().Split(';')[0])) 
                { 
                 var ID = new Guid(String.Concat("00000000-0000-0000-0000-", net.GetPhysicalAddress().ToString())); 
                } 
               } 
      else 
       { 

        NetworkInterface[] ifConfig = NetworkInterface.GetAllNetworkInterfaces(); 
        int maxHash = int.MinValue; 
        Guid D = Guid.Empty; 

        foreach (NetworkInterface net in ifConfig) 
        { 
         if (net.NetworkInterfaceType == NetworkInterfaceType.Ethernet) 
         { 
          if (maxHash < net.GetPhysicalAddress().ToString().GetHashCode()) 
          { 
           maxHash = net.GetPhysicalAddress().ToString().GetHashCode(); 

           var ID = new Guid(String.Concat("00000000-0000-0000-0000-", net.GetPhysicalAddress().ToString())); 

          } 
         } 
        }