如何在C#中快速获取硬件ID?
-
22-09-2019 - |
题
我需要在我的程序中将许可证与硬件 ID 绑定。我尝试使用 WMI,但仍然很慢。
例如,我需要 CPU、HDD 和主板信息。
解决方案
欲了解更多详情,请参阅 这个链接
以下代码将为您提供 CPU ID:
需要命名空间 System.Management
var mbs = new ManagementObjectSearcher("Select ProcessorId From Win32_processor");
ManagementObjectCollection mbsList = mbs.Get();
string id = "";
foreach (ManagementObject mo in mbsList)
{
id = mo["ProcessorId"].ToString();
break;
}
有关硬盘 ID 和主板 ID 的详细信息,请参阅 此链接
要加快此过程,请确保您不使用 SELECT *
, ,但只选择您真正需要的。使用 SELECT *
仅在开发过程中,当您尝试找出需要使用的内容时,因为那时查询将需要 很多 需要更长的时间才能完成。
其他提示
我来这里寻找同样的东西,然后找到了另一个解决方案。如果你们有兴趣,我分享这门课:
using System;
using System.Management;
using System.Security.Cryptography;
using System.Security;
using System.Collections;
using System.Text;
namespace Security
{
/// <summary>
/// Generates a 16 byte Unique Identification code of a computer
/// Example: 4876-8DB5-EE85-69D3-FE52-8CF7-395D-2EA9
/// </summary>
public class FingerPrint
{
private static string fingerPrint = string.Empty;
public static string Value()
{
if (string.IsNullOrEmpty(fingerPrint))
{
fingerPrint = GetHash("CPU >> " + cpuId() + "\nBIOS >> " +
biosId() + "\nBASE >> " + baseId() +
//"\nDISK >> "+ diskId() + "\nVIDEO >> " +
videoId() +"\nMAC >> "+ macId()
);
}
return fingerPrint;
}
private static string GetHash(string s)
{
MD5 sec = new MD5CryptoServiceProvider();
ASCIIEncoding enc = new ASCIIEncoding();
byte[] bt = enc.GetBytes(s);
return GetHexString(sec.ComputeHash(bt));
}
private static string GetHexString(byte[] bt)
{
string s = string.Empty;
for (int i = 0; i < bt.Length; i++)
{
byte b = bt[i];
int n, n1, n2;
n = (int)b;
n1 = n & 15;
n2 = (n >> 4) & 15;
if (n2 > 9)
s += ((char)(n2 - 10 + (int)'A')).ToString();
else
s += n2.ToString();
if (n1 > 9)
s += ((char)(n1 - 10 + (int)'A')).ToString();
else
s += n1.ToString();
if ((i + 1) != bt.Length && (i + 1) % 2 == 0) s += "-";
}
return s;
}
#region Original Device ID Getting Code
//Return a hardware identifier
private static string identifier
(string wmiClass, string wmiProperty, string wmiMustBeTrue)
{
string result = "";
System.Management.ManagementClass mc =
new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
if (mo[wmiMustBeTrue].ToString() == "True")
{
//Only get the first one
if (result == "")
{
try
{
result = mo[wmiProperty].ToString();
break;
}
catch
{
}
}
}
}
return result;
}
//Return a hardware identifier
private static string identifier(string wmiClass, string wmiProperty)
{
string result = "";
System.Management.ManagementClass mc =
new System.Management.ManagementClass(wmiClass);
System.Management.ManagementObjectCollection moc = mc.GetInstances();
foreach (System.Management.ManagementObject mo in moc)
{
//Only get the first one
if (result == "")
{
try
{
result = mo[wmiProperty].ToString();
break;
}
catch
{
}
}
}
return result;
}
private static string cpuId()
{
//Uses first CPU identifier available in order of preference
//Don't get all identifiers, as it is very time consuming
string retVal = identifier("Win32_Processor", "UniqueId");
if (retVal == "") //If no UniqueID, use ProcessorID
{
retVal = identifier("Win32_Processor", "ProcessorId");
if (retVal == "") //If no ProcessorId, use Name
{
retVal = identifier("Win32_Processor", "Name");
if (retVal == "") //If no Name, use Manufacturer
{
retVal = identifier("Win32_Processor", "Manufacturer");
}
//Add clock speed for extra security
retVal += identifier("Win32_Processor", "MaxClockSpeed");
}
}
return retVal;
}
//BIOS Identifier
private static string biosId()
{
return identifier("Win32_BIOS", "Manufacturer")
+ identifier("Win32_BIOS", "SMBIOSBIOSVersion")
+ identifier("Win32_BIOS", "IdentificationCode")
+ identifier("Win32_BIOS", "SerialNumber")
+ identifier("Win32_BIOS", "ReleaseDate")
+ identifier("Win32_BIOS", "Version");
}
//Main physical hard drive ID
private static string diskId()
{
return identifier("Win32_DiskDrive", "Model")
+ identifier("Win32_DiskDrive", "Manufacturer")
+ identifier("Win32_DiskDrive", "Signature")
+ identifier("Win32_DiskDrive", "TotalHeads");
}
//Motherboard ID
private static string baseId()
{
return identifier("Win32_BaseBoard", "Model")
+ identifier("Win32_BaseBoard", "Manufacturer")
+ identifier("Win32_BaseBoard", "Name")
+ identifier("Win32_BaseBoard", "SerialNumber");
}
//Primary video controller ID
private static string videoId()
{
return identifier("Win32_VideoController", "DriverVersion")
+ identifier("Win32_VideoController", "Name");
}
//First enabled network card ID
private static string macId()
{
return identifier("Win32_NetworkAdapterConfiguration",
"MACAddress", "IPEnabled");
}
#endregion
}
}
我不会因此获得任何荣誉,因为我发现了它 这里它的效果比我预期的要快。没有显卡、mac 和驱动器 ID,我在大约 2-3 秒内获得了唯一 ID。加上上面的内容,我在大约 4-5 秒内就得到了它。
笔记: 添加参考 System.Management
.
下面的方法是由激发此答案以一个相关的(更一般的)问题。
的方法是读取注册表项MachineGuid
的HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Cryptography
值。操作系统安装期间产生该值。
有围绕使用这种方法每台机器的硬件ID的唯一性几种方法。一种方法是编辑注册表值,但这之后会导致用户机器上的并发症。另一种方法是克隆这将复制MachineGuid
值的驱动图像。
但是,没有一种方法是黑客,防这肯定将是对于普通用户来说足够好。从有利的一面,这种方法是快速的性能,明智和实现简单。
public string GetMachineGuid()
{
string location = @"SOFTWARE\Microsoft\Cryptography";
string name = "MachineGuid";
using (RegistryKey localMachineX64View =
RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
{
using (RegistryKey rk = localMachineX64View.OpenSubKey(location))
{
if (rk == null)
throw new KeyNotFoundException(
string.Format("Key Not Found: {0}", location));
object machineGuid = rk.GetValue(name);
if (machineGuid == null)
throw new IndexOutOfRangeException(
string.Format("Index Not Found: {0}", name));
return machineGuid.ToString();
}
}
}
这里是一个DLL,它示出了:结果, *硬盘ID(写入驱动器的IDE电子芯片固有硬件序列号)点击 *分区ID(卷序列号),点击 * CPU ID(唯一的硬件ID),点击 * CPU厂商结果 * CPU运行速度点击 * CPU理论速度点击 *内存负载(总内存在使用百分比(%))点击 *总的物理(以字节为单位的总物理内存)点击 *担保条件物理(物理内存留以字节为单位)点击 *总页面文件(以字节为单位的总页面文件)点击 *可用页面文件(左字节页文件)点击 *总的虚拟(总虚拟字节内存)点击 *可用虚拟(左字节虚拟内存)点击 * BIOS唯一标识numberBiosDate结果 * BIOS唯一标识numberBiosVersion结果 * BIOS唯一标识numberBiosProductID结果 * BIOS唯一标识numberBiosVideo
(文本从原始网站抓起),点击 它与C#。