.NET 8 获取CPU序列号和主板序列号异常问题

一般情况下我们会使用:

获取磁盘序列号:

try

{

System.Management.ManagementObjectSearcher cmicWmi = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");

System.UInt32 tmpUint32 = 0;

foreach(ManagementObject cmicWmiObj in cmicWmi.Get())

{

tmpUint32 = Convert.ToUInt32(cmicWmiObj"signature".ToString());

}

this.textBox1.Text = tmpUint32.ToString();

this.gProgressBar1.Value ++;

}

catch(Exception ex1)

{

throw new Exception(ex1.ToString());

}

获取CPU序列号:

try

{

System.Management.ManagementObjectSearcher Wmi = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");

string tmpUint32_1 = string.Empty;

foreach(ManagementObject WmiObj in Wmi.Get())

{

tmpUint32_1 =WmiObj"ProcessorId".ToString();

}

this.textBox2.Text = tmpUint32_1;

this.gProgressBar1.Value ++;

}

catch(Exception ex2)

{

throw new Exception(ex2.ToString());

}

如上图代码,可能会在 winserver 2016 2019 2012r2 某些系统上报错,其错误信息为:

System.TypeInitializationException: The type initializer for 'System.Management.ManagementPath' threw an exception. ---> System.PlatformNotSupportedException: The native library 'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\wminet_utils.dll' does not have all required functions. Please, update the .NET Framework.

因为在windows 平台上使用这段代码,其实是依赖了.NET FrameWork 相关的,为防止错误异常情况,微软有新的库对访问硬件信息的支持,

可在nuget 使用 Microsoft.Management.Infrastructure

其通过.NET8获取cpu序列号和主板序列号代码如下:

#region 获取处理器序列号

public static string cpuinfo()

{

// 查询 WMI 类

string namespaceName = @"root\cimv2";

string query = "SELECT * FROM Win32_BaseBoard";

CimSession session = CimSession.Create(null);

IEnumerable<CimInstance> result = session.QueryInstances(namespaceName, "WQL", query);

string boardSerialNumber = string.Empty;

foreach (CimInstance instance in result)

{

boardSerialNumber = instance.CimInstanceProperties"SerialNumber".Value.ToString();

}

return boardSerialNumber;

}

#endregion

#region 获取主板序列号

public static string biosinfo()

{

string namespaceName = @"root\cimv2";

string query = "SELECT * FROM Win32_Processor";

CimSession session = CimSession.Create(null);

IEnumerable<CimInstance> result = session.QueryInstances(namespaceName, "WQL", query);

string BiosSerialNumber = string.Empty;

foreach(CimInstance instance in result)

{

BiosSerialNumber = instance.CimInstanceProperties"ProcessorId".Value.ToString();

}

return BiosSerialNumber;

}

#endregion

上块代码经过实测和 .NET framework 获取的序列号是一致的。

相关推荐
极光通讯6 分钟前
呆滞库存回收定价逻辑:如何评估DDR4内存和SATA SSD的回收价值
服务器·ssd
技术人生0016 分钟前
VMware Workstation 安装 Ubuntu 24.04 LTS 完整教程
运维·服务器·ubuntu
程序员清风26 分钟前
Java 智能体开发:从对话接口到任务执行
java·人工智能·python
YumiProxy27 分钟前
动态IP冲突排查实战:从ARP检测到DHCP地址池的完整链路
服务器·网络·网络协议·tcp/ip·ip
一 乐34 分钟前
博客管理系统|基于springboot + vue博客管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·毕设
ai小陈37 分钟前
GPU服务器租用容器实战:Docker数据卷持久化与安全重建
服务器·人工智能·安全·docker·ai·gpu算力
QuZhengRong40 分钟前
【Spring】后端接收的请求参数多了一个逗号的处理办法
java·后端·spring
ZealSinger41 分钟前
Go1.25容器感知GOMAXPROCS避限流
java·开发语言
cxoptics1 小时前
LBO 会不会潮解?存储、镀膜、日常使用注意事项?
java
SL_staff1 小时前
非研发人员搭业务逻辑?先认清这3个可视化编程分水岭
java