Windows 7中的MAC地址 [英] MAC address in Windows 7

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

问题描述

您好,先生

请帮助我解决这两个问题

1.下面的代码显示了XP中的mac地址,但在Windows7中为空字符串.
2.是否有可能直接通过查询查找mac地址,而不是循环" foreach "语句.



Hello sir

Please help me in these two issues

1. Below code shows mac address in XP but empty string in Windows7.
2. Is there any chance to find mac address directly with a query instead of looping "foreach" statement.



string MACAddress = String.Empty;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();                
                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(":", "");
                return MACAddress;

推荐答案

检查一下

Check this out

/// <summary>
/// returns the mac address of the first operation nic found.
/// </summary>
/// <returns></returns>
private string GetMacAddress()
{
    string macAddresses = "";

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



更多信息在这里:
http://msdn.microsoft.com/en-us/library/system. net.networkinformation.networkinterface.aspx [ ^ ]

干杯!



More info here:
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.aspx[^]

Cheers!


尊敬的先生,谢谢您的答复.

当电缆插入时,我得到了mac地址,但是当我拔出电缆时,得到了相同的错误(空字符串).我的大多数客户都没有网络连接.

有没有其他方法可以在Win 7中获得Mac地址?
dear sir thanks for the reply.

i got the mac address when the cable is plugged, but when i unplugged it got the same error(empty string). Most of my clients are not having net connection.

is there any alternate way to get mac address in win 7?


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

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