.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 获取的序列号是一致的。

相关推荐
Xiaokai丶19 分钟前
Java 8 新特性深度剖析:核心要点与代码实战
java
灵魂猎手21 分钟前
3. MyBatis Executor:SQL 执行的核心引擎
java·后端·源码
Galaxy在掘金21 分钟前
从业8年,谈谈我认知的后端架构之路-1
java·架构
许泽宇的技术分享38 分钟前
Windows MCP.Net:基于.NET的Windows桌面自动化MCP服务器深度解析
windows·自动化·.net
努力努力再努力wz1 小时前
【c++深入系列】:万字详解模版(下)
java·c++·redis
瓦特what?2 小时前
关于C++的#include的超超超详细讲解
java·开发语言·数据结构·c++·算法·信息可视化·数据挖掘
是乐谷3 小时前
阿里云杭州 AI 产品法务岗位信息分享(2025 年 8 月)
java·人工智能·阿里云·面试·职场和发展·机器人·云计算
Java水解3 小时前
Java中的四种引用类型详解:强引用、软引用、弱引用和虚引用
java·后端
Demisse3 小时前
[Linux] Linux文件系统基本管理
linux·运维·服务器
lifallen3 小时前
JCTools 无锁并发队列基础:ConcurrentCircularArrayQueue
java·开发语言·数据结构·算法