简体   繁体   English

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

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

I am using visual studio for mac using c#.我正在使用 c# 使用 Visual Studio for mac。 I have tried system.net.networkinformation but it gives me blank string.我试过 system.net.networkinformation 但它给了我空白字符串。

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

This is what i am using to get mac address.这就是我用来获取mac地址的方法。 It works fine in windows desktop application but not working in os x.它在 Windows 桌面应用程序中运行良好,但在 os x 中不起作用。 I am trying to get mac address of mac desktop for its unique identification.我正在尝试获取 mac 桌面的 mac 地址以获取其唯一标识。

Below function gives exact mac addresses which i need.下面的函数给出了我需要的确切 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

            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM