需要使用到的包
System.Management
注意: Cpu在同一型号下, 编码一致!!!
代码如下:
using System.Management;
namespace Test
{
class MachineCode
{
/// <summary>
/// 获取CPU的硬件编码
/// </summary>
/// <returns>CPU编码</returns>
public static string GetCpuCode()
{
string CpuCode = null;
ManagementClass CpuInfo = new ManagementClass("Win32_Processor");
foreach (ManagementObject i in CpuInfo.GetInstances())
{
CpuCode += i.Properties["ProcessorId"].Value.ToString().Replace(" ", "");
}
return CpuCode;
}
/// <summary>
/// 获取硬盘的硬件编码
/// </summary>
/// <returns>硬盘编码</returns>
public static string GetDiskCode()
{
string DiskCode = null;
ManagementClass DiskInfo = new ManagementClass("Win32_DiskDrive");
foreach (ManagementObject i in DiskInfo.GetInstances())
{
DiskCode += i.Properties["SerialNumber"].Value.ToString().Replace(" ", "");
}
return DiskCode;
}
/// <summary>
/// 获取内存的硬件编码
/// </summary>
/// <returns>内存编码</returns>
public static string GetRamCode()
{
string RamCode = null;
ManagementClass RamInfo = new ManagementClass("Win32_PhysicalMemory");
foreach (ManagementObject i in RamInfo.GetInstances())
{
RamCode += i.Properties["PartNumber"].Value.ToString().Replace(" ", "");
}
return RamCode;
}
/// <summary>
/// 获取主板的硬件编码
/// </summary>
/// <returns>主板编码</returns>
public static string GetMotherboardCode()
{
string MotherboardCode = null;
ManagementClass MotherboardInfo = new ManagementClass("Win32_BaseBoard");
foreach(ManagementObject i in MotherboardInfo.GetInstances())
{
MotherboardCode += i.Properties["SerialNumber"].Value.ToString();
}
return MotherboardCode;
}
}
}
运行结果:
文章评论