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

相关推荐
KobeSacre几秒前
CyclicBarrier 源码
java·jvm·算法
我登哥MVP18 分钟前
Hadoop成长史-从Nutch子项目到大数据生态王者
java·大数据·hadoop·分布式·云原生·云计算
jsons119 分钟前
rocky8内网离线批量补丁(你之前搭建的架构,多台服务器首选,无订阅)
linux·运维·服务器
进击切图仔22 分钟前
SAM3 微调标注流水线和 Label Studio
java·服务器·前端
java1234_小锋29 分钟前
Maven 4 要来了:15 年后,Java 构建工具迎来“彻底重构“
java·重构·maven
Bomangedd29 分钟前
NSK RA25BN 滚子直线导轨技术详解
运维·服务器·经验分享·规格说明书
霸道流氓气质38 分钟前
Java 工程师 AI 智能体学习路线 · 阶段 1:AI / LLM 基础认知 详解
java·人工智能·学习
JerrySir1 小时前
`spring.threads.virtual.enabled=true`:用三类负载定位等待、争用与拒绝
java·spring boot
尚早立志1 小时前
Spring Boot 之 BeanDefinitionLoader 详解
java·spring boot·后端
月光有害1 小时前
DDD 核心概念梳理:从领域模型到领域事件
java·ddd