我们有时遇到安装软件需要授权文件,就是利用电脑的硬件信息,来创建的授权文件的。加密和解密是很复杂的,可以独立一篇文章介绍一下,本文具体介绍C#获取电脑硬件信息,例如CPU信息,硬盘信息,RAM信息,主板信息等。
获取CPU信息:
_ProcessorId: BFEBFBFF000806EC
_
Name: Intel® Core™ i5-10210U CPU @ 1.60GHz
Manufacturer: GenuineIntel
MaxClockSpeed: 2112
实现方法如下:
using System.Management;// 添加引用
private static string Identifier(string wmiClass, string wmiPropert)
{
string str = “”;
ManagementClass Mcls = new ManagementClass(wmiClass);
foreach (ManagementObject obj in Mcls.GetInstances())
{
try
{
if (obj[wmiPropert] != null)
{
str = obj[wmiPropert].ToString();
}
return str;
}
catch (Exception)
{
}
}
return str;
}
测试上方法:
Identifier(“Win32_Processor”, “Name”);
返回字符串:Intel® Core™ i5-10210U CPU @ 1.60GHz
Identifier(“Win32_Processor”, “ProcessorId”);
返回字符串:BFEBFBFF000806EC
Identifier(“Win32_Processor”, “Manufacturer”);
返回字符串:GenuineIntel
Identifier(“Win32_Processor”, “MaxClockSpeed”);
返回字符串:2112
BIOS信息:
_Manufacturer: LENOVO
_
SMBIOSBIOSVersion: N2XET29W (1.19 )
SerialNumber: PF2XADPM
ReleaseDate:20210323000000.000000+000
Version: LENOVO - 1190
测试代码如下:
string s1 = Identifier(“Win32_BIOS”, “Manufacturer”);
if (s1 != “”)
ShowMessage("Manufacturer: " + s1);
string s2 = Identifier(“Win32_BIOS”, “SMBIOSBIOSVersion”);
if (s2 != “”)
ShowMessage("SMBIOSBIOSVersion: " + s2);
string s3 = Identifier(“Win32_BIOS”, “IdentificationCode”);
if (s3 != “”) //这个字段返回为空
ShowMessage("IdentificationCode: " + s3);
string s4 = Identifier(“Win32_BIOS”, “SerialNumber”);
if (s4 != “”)
ShowMessage("SerialNumber: " + s4);
string s5 = Identifier(“Win32_BIOS”, “ReleaseDate”);
if (s5 != “”)
ShowMessage(“ReleaseDate:” + s5);
string s6 = Identifier(“Win32_BIOS”, “Version”);
if (s6 != “”)
ShowMessage("Version: " + s6);
硬盘信息:
_Model: INTEL HBRPEKNX0202AL
_
Manufacturer: (标准磁盘驱动器)
Signature: 0
TotalHeads: 255
封装的方法如下:
static ManagementObjectCollection GetIdentifierCollection(string wmiClass)
{
ManagementClass cls = new ManagementClass(wmiClass);
return cls.GetInstances();
}
//单独列出来,避免USB Disk计算在内
static string GetDiskIdentifier(ManagementObjectCollection moc, string wmiProperty)
{
string s = “”;
foreach (ManagementObject obj in moc)
{
if ((obj[“InterfaceType”] == null) || (obj[“InterfaceType”].ToString() != “USB”))
{
try
{
if (obj[wmiProperty] != null)
{
s = obj[wmiProperty].ToString();
}
}
catch (Exception)
{
}
}
}
return s;
}
测试代码如下:
ManagementObjectCollection mocs = GetIdentifierCollection(“Win32_DiskDrive”);
string s1 = GetDiskIdentifier(mocs, “Model”);
if (s1 != “”)
ShowMessage("Model: " + s1);
string s2 = GetDiskIdentifier(mocs, “Manufacturer”);
if (s2 != “”)
ShowMessage("Manufacturer: " + s2);
string s3 = GetDiskIdentifier(mocs, “Signature”);
if (s3 != “”)
ShowMessage("Signature: " + s3);
string s4 = GetDiskIdentifier(mocs, “TotalHeads”);
if (s4 != “”)
ShowMessage("TotalHeads: " + s4);
核心是封装的函数,这个可以更具自己的需求做更改,下面的测试代码是验证功能函数获取硬件信息的。
获取主板信息:
_Manufacturer: LENOVO
_
Name: 基板
SerialNumber: L1HF15D03J2
功能函数如下:
ManagementObjectCollection Mocs = GetIdentifierCollection(“Win32_BaseBoard”);
string firstIdentifier = GetFirstIdentifier(Mocs, “Model”);
if (firstIdentifier != “”)
ShowMessage("Model: " + firstIdentifier);
string s1 = GetFirstIdentifier(Mocs, “Manufacturer”);
if (s1 != “”)
ShowMessage("Manufacturer: " + s1);
string s2 = GetFirstIdentifier(Mocs, “Name”);
if (s2 != “”)
ShowMessage("Name: " + s2);
string s3 = GetFirstIdentifier(Mocs, “SerialNumber”);
if (s3 != “”)
ShowMessage("SerialNumber: " + s3);
获取主板的制造商,主板序列号,主板名称,这信息都很有用的,电脑出厂后,这些信息不换硬件,是不会改变的。利用这些信息创建的授权码都是依赖这个硬件信息计数出一个加密序列码的。