弄电脑的MAC地址 [英] get MAC address of computer

查看:152
本文介绍了弄电脑的MAC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想accesss使用C#的计算机的MAC地址。我使用了下列code访问MAC地址,但有这code一些问题。

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(":", "");

在第一个code当我们从网络连接断开,那么它将返回空的MAC地址。第二code返回时网络适配器连接ID断开的MAC地址,但是,当我们disbled网络连接或删除计算机的IP地址,然后它会返回空的MAC地址。

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 .

如何得到MAC地址时,网络连接系统,禁用,有一个分配给PC或网络连接没有IP地址,断开连接?

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

推荐答案

当您禁用网络适配器,你不能访问它在所有 - 这是因为如果没有安装,这就是为什么你不这样做看到一个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.

编辑:说明:

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

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.

这篇关于弄电脑的MAC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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