dotnet 通过 WMI 获取系统信息

2022-08-07 13:20:25 浏览数 (1)

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

通过 Win32_OperatingSystem 可以获取系统信息

代码语言:javascript复制
            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 的内容

代码语言:javascript复制
BootDevice           : DeviceHarddiskVolume2
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|DeviceHarddisk0Partition4
Organization         :
OSArchitecture       : 64 位
OtherTypeDescription :
PlusProductID        :
PlusVersionNumber    :
RegisteredUser       : lindexi_gd@outlook.com
SerialNumber         : 00331-10000-00001-AA523
Status               : OK
SystemDevice         : DeviceHarddiskVolume4
SystemDirectory      : C:WINDOWSsystem32
SystemDrive          : C:
Version              : 10.0.17763
WindowsDirectory     : C:WINDOWS

这里的 Version 就是系统版本

Win32_OperatingSystem class - Windows applications

0 人点赞