简体   繁体   English

禁用网络适配器时获取MAC地址?

[英]Get MAC Address when network adapter is disabled?

Is there any way i can retrieve MAC Address when Network Adapter is disabled in .net? 有什么办法可以在.net中禁用网络适配器时检索MAC地址吗?

Thanks in advance, 提前致谢,

It is not possible to get the MAC address of an adapter which is disabled: this is because getting the MAC address requires querying the driver, and the driver for a disabled adapter is not loaded ( source ). 无法获取已禁用的适配器的MAC地址:这是因为获取MAC地址需要查询驱动程序,并且未加载已禁用适配器的驱动程序( )。

You can, however get the MAC address of an adapter which is not currently connected. 但是,您可以获取当前未连接的适配器的MAC地址。

The WMI route is no good here, because it shows the MAC address as null for adapters which are not connected. WMI路由在这里并不好,因为它显示未连接的适配器的MAC地址为空。 The good news is that the NetworkInterface.GetAllNetworkInterfaces() route works just fine: 好消息是NetworkInterface.GetAllNetworkInterfaces()路由工作得很好:

// using System.Net.NetworkInformation;
var nics = NetworkInterface.GetAllNetworkInterfaces();

// pick your NIC!
var selectedNic = nics.First();

var macAddress = selectedNic.GetPhysicalAddress().ToString();

You can use WMI: 您可以使用WMI:

public static string GetMACAddress()
    {
        ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
        ManagementObjectCollection moc = mc.GetInstances();
        string MACAddress=String.Empty;
        foreach(ManagementObject mo in moc)
        {
            if(MACAddress==String.Empty)  // only return MAC Address from first card
            {
                MACAddress= mo["MacAddress"].ToString() ;
            }
            mo.Dispose();
        }

        return MACAddress;
    }

Refer this link. 请参阅此链接。

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx http://msdn.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx

The example here displays physical address of all interface irrespective of their operational stage. 此处的示例显示所有接口的物理地址,而不管其操作阶段。 HTH. HTH。

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

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