编程语言
首页 > 编程语言> > C#获取 mac

C#获取 mac

作者:互联网

C#获取 mac

public static String getLocalMacAddress()
{//没有缓存的地址,则查询
String mac_s = “”;

        IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        if (nics == null || nics.Length < 1)
        {

            return mac_s;
        }

        try
        {

            foreach (NetworkInterface adapter in nics)
            {
                IPInterfaceProperties properties = adapter.GetIPProperties(); //  .GetIPInterfaceProperties();
                string Description = adapter.Description;//接口的描述
                string NetworkInterfaceType = adapter.NetworkInterfaceType.ToString();//获取接口类型
                PhysicalAddress address = adapter.GetPhysicalAddress();//返回MAC地址
                if (NetworkInterfaceType != "Loopback" && NetworkInterfaceType == "Ethernet")

                    mac_s= adapter.GetPhysicalAddress().ToString();

            }

标签:String,C#,nics,adapter,NetworkInterface,获取,mac,NetworkInterfaceType
来源: https://blog.csdn.net/w6827928/article/details/114749944