如何使用 C# 获取 mac os x 设备的物理地址

How do i get Physical Address of a mac os x device by using c#

我使用 visual studio 作为 mac 使用 c#。我试过 system.net.networkinformation 但它给了我空白字符串。

foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (nic.OperationalStatus == OperationalStatus.Up)
                {
                    mac += nic.GetPhysicalAddress().ToString();
                    //Console.WriteLine(mac);
                    break;
                }
            }

这就是我用来获取 mac 地址的方法。它在 windows 桌面应用程序中工作正常但在 os x 中不工作。我正在尝试获取 mac 桌面的 mac 地址作为其唯一标识。

下面的函数给出了我需要的确切 mac 地址。

  public static string GetMacAddress()
        {
            System.Net.NetworkInformation.NetworkInterfaceType Type = 0;
            string MacAddress = string.Empty;
            try
            {
                System.Net.NetworkInformation.NetworkInterface[] theNetworkInterfaces = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces();
                foreach (System.Net.NetworkInformation.NetworkInterface currentInterface in theNetworkInterfaces)
                {
                    Type = currentInterface.NetworkInterfaceType;
                    if (Type == System.Net.NetworkInformation.NetworkInterfaceType.Ethernet || Type == System.Net.NetworkInformation.NetworkInterfaceType.GigabitEthernet || Type == System.Net.NetworkInformation.NetworkInterfaceType.FastEthernetFx)
                    {
                        MacAddress = currentInterface.GetPhysicalAddress().ToString();
                        break;
                    }
                }

                return MacAddress;

            }
            catch 
            {
                // your code goes here

            }

        }