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

相关推荐
安之若素^9 分钟前
启用不安全的HTTP方法
java·开发语言
ruanjiananquan9915 分钟前
c,c++语言的栈内存、堆内存及任意读写内存
java·c语言·c++
喜欢吃豆19 分钟前
快速手搓一个MCP服务指南(九): FastMCP 服务器组合技术:构建模块化AI应用的终极方案
服务器·人工智能·python·深度学习·大模型·github·fastmcp
ChaITSimpleLove28 分钟前
.NET9 实现斐波那契数列(FibonacciSequence)性能测试
.net·性能测试·斐波那契数列·fibonacci·benchmarkdotnet·datadog.trace
chuanauc42 分钟前
Kubernets K8s 学习
java·学习·kubernetes
一头生产的驴1 小时前
java整合itext pdf实现自定义PDF文件格式导出
java·spring boot·pdf·itextpdf
YuTaoShao1 小时前
【LeetCode 热题 100】73. 矩阵置零——(解法二)空间复杂度 O(1)
java·算法·leetcode·矩阵
zzywxc7871 小时前
AI 正在深度重构软件开发的底层逻辑和全生命周期,从技术演进、流程重构和未来趋势三个维度进行系统性分析
java·大数据·开发语言·人工智能·spring
九丝城主1 小时前
2025使用VM虚拟机安装配置Macos苹果系统下Flutter开发环境保姆级教程--上篇
服务器·flutter·macos·vmware
南瓜胖胖2 小时前
【seismic unix相速度分析-频散曲线】
服务器·unix