本文告诉大家如何通过 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


本文会经常更新,请阅读原文: https://dotnet-campus.github.io//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.html ,以避免陈旧错误知识的误导,同时有更好的阅读体验。

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