2017-08-16 64 views
0

我正在嘗試從計算機檢索板載以太網適配器的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。 –

回答

0

作爲選項(不是最好的,但仍然=)) - 您可以嘗試使用 度量標準。在大多數情況下,網絡到物理網絡的度量值 卡優先級

  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())); 

          } 
         } 
        } 
相關問題