How to determine MAC Address of the actual physical network card — not virtual network interfaces created by VPN's (.NET C#)

前端 未结 5 1971
我在风中等你
我在风中等你 2020-12-01 07:00

Background

I\'m trying to get obtain a unique identifier out of a computer and want to be able to reliably return the same MAC address each time. Trust me I have

5条回答
  •  囚心锁ツ
    2020-12-01 07:31

    This is my method: it uses the fact that physical card is connected to PCI interface

    ManagementObjectSearcher searcher = new ManagementObjectSearcher
        ("Select MACAddress,PNPDeviceID FROM Win32_NetworkAdapter WHERE MACAddress IS NOT NULL AND PNPDeviceID IS NOT NULL");
    ManagementObjectCollection mObject = searcher.Get();
    
    foreach (ManagementObject obj in mObject)
    {
        string pnp = obj["PNPDeviceID"].ToString();
        if (pnp.Contains("PCI\\"))
        {
            string mac = obj["MACAddress"].ToString();
            mac = mac.Replace(":", string.Empty);
            return mac;
        }
    }
    

提交回复
热议问题