如何获取系统的MAC ID [英] How to get MAC id of system

查看:320
本文介绍了如何获取系统的MAC ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取系统的MAC ID

How can i get MAC ID of system

推荐答案

尝试以下操作:
Try this:
private string GetMac()
{
    string Mac = string.Empty;
    ManagementClass MC = new ManagementClass("Win32_NetworkAdapter");
    ManagementObjectCollection MOCol = MC.GetInstances();
    foreach (ManagementObject MO in MOCol)
        if (MO != null)
        {
           if (MO["MacAddress"] != null)
                    {
                         Mac = MO["MACAddress"].ToString();
                         if (Mac != string.Empty)
                             break;
                    }
        }
    return Mac;
}



以下是代码-

以最快的速度返回NIC并具有有效的Mac地址.
Hi,
following is the code-

returns the NIC with the fastest speed that also has a valid Mac Address.
private string GetMacAddress()
    {
        const int MIN_MAC_ADDR_LENGTH = 12;
        string macAddress = "";
        long maxSpeed=-1;

        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            log.Debug("Found MAC Address: " + nic.GetPhysicalAddress().ToString() + " Type: " + nic.NetworkInterfaceType );
            string tempMac = nic.GetPhysicalAddress().ToString();
            if (nic.Speed > maxSpeed && !String.IsNullOrEmpty(tempMac) && tempMac.Length >= MIN_MAC_ADDR_LENGTH )
            {
                log.Debug("New Max Speed = " + nic.Speed + ", MAC: " + tempMac );
                maxSpeed = nic.Speed;
                macAddress = tempMac;
            }
        }
        return macAddress;
    }




只返回第一个.




Just returns the first one.

private string GetMacAddress()
    {
        string macAddresses = "";

        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (nic.OperationalStatus == OperationalStatus.Up)
            {
                macAddresses += nic.GetPhysicalAddress().ToString();
                break;
            }
        }
        return macAddresses;
    }


这篇关于如何获取系统的MAC ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆