Skip to content

dotnet 通过 WMI 获取系统信息

Updated: at 08:22,Created: at 08:55

本文告诉大家如何通过 WMI 获取系统信息

通过 Win32_OperatingSystem 可以获取系统信息

var mc = "Win32_OperatingSystem";
var managementObject = new[]
{
"BootDevice",
"BuildNumber",
"BuildType",
"Caption",
"CodeSet",
"CountryCode",
"CreationClassName",
"CSCreationClassName",
"CSDVersion",
"CSName",
"Description",
"Locale",
"Manufacturer",
"Name",
"Organization",
"OSArchitecture",
"OtherTypeDescription",
"PlusProductID",
"PlusVersionNumber",
"RegisteredUser",
"SerialNumber",
"Status",
"SystemDevice",
"SystemDirectory",
"SystemDrive",
"Version",
"WindowsDirectory",
};
ManagementClass managementClass = new ManagementClass(mc);
ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
var str = new StringBuilder();
foreach (ManagementObject m in managementObjectCollection)
{
foreach (var temp in managementObject)
{
try
{
str.Append(temp);
str.Append(" ");
str.Append(m[temp]?.ToString() ?? "");
str.Append("\n");
}
catch (Exception e)
{
Console.WriteLine(temp + " " + e);
}
}
str.Append("\n");
}
return str.ToString();

输出 str 的内容

BootDevice : \Device\HarddiskVolume2
BuildNumber : 17763
BuildType : Multiprocessor Free
Caption : Microsoft Windows 10 专业版
CodeSet : 936
CountryCode : 86
CreationClassName : Win32_OperatingSystem
CSCreationClassName : Win32_ComputerSystem
CSDVersion :
CSName : DESKTOP-KA1CD6M
Description :
Locale : 0804
Manufacturer : Microsoft Corporation
Name : Microsoft Windows 10 专业版|C:\WINDOWS|\Device\Harddisk0\Partition4
Organization :
OSArchitecture : 64
OtherTypeDescription :
PlusProductID :
PlusVersionNumber :
RegisteredUser : lindexi_gd@outlook.com
SerialNumber : 00331-10000-00001-AA523
Status : OK
SystemDevice : \Device\HarddiskVolume4
SystemDirectory : C:\WINDOWS\system32
SystemDrive : C:
Version : 10.0.17763
WindowsDirectory : C:\WINDOWS

这里的 Version 就是系统版本

Win32_OperatingSystem class - Windows applications


知识共享许可协议

原文链接: http://blog.lindexi.com/post/dotnet-%E9%80%9A%E8%BF%87-WMI-%E8%8E%B7%E5%8F%96%E7%B3%BB%E7%BB%9F%E4%BF%A1%E6%81%AF

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。 欢迎转载、使用、重新发布,但务必保留文章署名 林德熙 (包含链接: https://blog.lindexi.com ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我 联系