获取计算机的MAC地址(get MAC address of computer)

系统教程 行业动态 更新时间:2024-06-14 17:01:34
获取计算机的MAC地址(get MAC address of computer)

我想使用c#访问计算机的mac地址。 我使用以下代码访问mac地址,但此代码中存在一些问题。

代码1

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

代码2

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 { if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString(); } mo.Dispose(); } MACAddress = MACAddress.Replace(":", "");

在第一个代码中,当我们断开网络连接时,它将返回空的mac地址。 当网络适配器连接ID断开时,第二个代码返回mac地址但是当我们断开网络连接或删除计算机的IP地址时,它将返回空的mac地址。

如何在禁用网络连接时获取mac地址,没有分配给PC的IP地址或网络连接断开?

I want to accesss the mac address of the computer using c#. I have used the following code to access mac address but there is some issue in this code.

Code 1

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

Code 2

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 { if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString(); } mo.Dispose(); } MACAddress = MACAddress.Replace(":", "");

In first code when we disconnected from network connection then it will return null mac address . second code return the mac address when network adapter connection id disconnected But when we disbled the network connection or remove the IP address of the computer then it will return the null mac address .

How to get the mac address when network connnection disabled, there is no IP address assigned to the PC or network connection disconnected?

最满意答案

当您禁用网络适配器时,根本无法访问它 - 就好像它没有安装一样,这就是您没有看到MAC地址的原因。

编辑:说明:

MAC地址属于网络适配器。 如果您有3个适配器,则有3个MAC地址。 如果没有适配器,则没有MAC地址。

When you disable your network adapter, you can't access it at all - it is as if it isn't installed, which is why you don't see a MAC address.

EDIT: Explanation:

A MAC address belongs to a network adapter. If you have 3 adapters you have 3 MAC addresses. If you have no adapters, you have no MAC address.

更多推荐

本文发布于:2023-04-20 18:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/3aade08cd0caf10963d89e3d8f53b2de.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:地址   计算机   computer   MAC   address

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!