【问题标题】:Get MAC Address using ManagementObjectSearcher使用 ManagementObjectSearcher 获取 MAC 地址
【发布时间】:2015-07-24 06:37:48
【问题描述】:

我正在尝试在 C# 中开发注册算法。我使用客户端机器的 MAC 地址来生成请求代码。功能如下图。但在 Windows 7 中,此函数在此行显示 NullRererenceException

mac = mo["MACAddress"].ToString();

public string GetMACAddress()
{
      string mac = null;
      ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_NetworkAdapterConfiguration");
      foreach (ManagementObject mo in mos.Get())
      {
           mac = mo["MACAddress"].ToString();
           break;
      }

      return mac;
}

在 Windows 7 和 Windows 8 中,使用 C# 获取 MAC 地址以开发激活算法的最可靠方法是什么?

【问题讨论】:

  • 你想要哪一个?可能有几个适配器,而您只使用第一个,无论它是什么。可能与操作系统无关,而是机器设置
  • 我想获取以太网 MAC 地址。原因是,有些台式电脑没有无线适配器。

标签: c# windows-8 windows-7 wmi mac-address


【解决方案1】:

并非所有对象都包含 MAC 地址,因此需要检查哪一剂具有 MAC

你可以这样做

string macAddress = String.Empty;
foreach (ManagementObject mo in mos.Get())
 {
      object tempMacAddrObj = MO["MacAddress"];

    if (tempMacAddrObj == null) //Skip objects without a MACAddress
    {
        continue;
    }
    if (macAddress == String.Empty) // only return MAC Address from first card that has a MAC Address
    {
        macAddress = tempMacAddrObj.ToString();              
    }
    objMO.Dispose();
 } 

【讨论】:

    【解决方案2】:

    出于激活许可证的目的,我实际上建议在 MAC 地址之外使用其他内容(或附加内容),因为这很容易被欺骗。这是一个非常不错的 C# 教程,介绍了如何获取应该可以解决您的问题的“硬件指纹”:http://www.codeproject.com/Articles/28678/Generating-Unique-Key-Finger-Print-for-a-Computer

    【讨论】:

      猜你喜欢
      • 2013-05-05
      • 2014-07-24
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 2014-07-12
      • 1970-01-01
      相关资源
      最近更新 更多