我正试图在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地址最可靠的方法是什么,以便开发激活算法?
发布于 2015-05-13 06:20:50
并不是所有的对象都包含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();
} 发布于 2015-05-13 07:10:01
为了许可证激活的目的,我实际上建议在MAC地址上使用其他的东西(或者是另外的),因为这很容易被欺骗。这里有一个非常好的C#教程,介绍如何获得解决问题的“硬件指纹”:http://www.codeproject.com/Articles/28678/Generating-Unique-Key-Finger-Print-for-a-Computer
https://stackoverflow.com/questions/30206953
复制相似问题