C# 通过WMI 获取硬件信息

// 关于WMI https://learn.microsoft.com/zh-cn/windows/win32/wmisdk/about-wmi
using System.Management;              // 使用WMI管理类, 需要从NuGet库中下载.


           ManagementClass aboutMe = new ManagementClass("win32_processor");
           // 参考: https://learn.microsoft.com/zh-cn/dotnet/api/system.management.managementclass.-ctor?view=dotnet-plat-ext-6.0#system-management-managementclass-ctor
           ManagementObjectCollection moc = aboutMe.GetInstances();
           foreach (ManagementObject x in moc) { label1.Text = $"CPU编号: {x["processorID"]}"; }

           ManagementObjectSearcher driveID = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");        // 使用了LINQ语法
           /*         常用的WMI查询语句
                       SELECT* FROM Win32_UserAccount 获取Windows用户信息
                       SELECT* FROM Win32_Group 获取用户组别信息
                       SELECT* FROM Win32_Process 获取当前进程信息
                       SELECT* FROM Win32_Service 获取系统服务信息
                       SELECT* FROM Win32_SystemDriver 获取系统驱动信息
                       SELECT* FROM Win32_Processor 获取CPU信息
                       SELECT* FROM Win32_BaseBoard 获取主板信息
                       SELECT* FROM Win32_BIOS 获取BIOS信息
                       SELECT* FROM Win32_VideoController 获取显示信息
                       SELECT* FROM Win32_SoundDevice 获取音频设备信息
                       SELECT* FROM Win32_PhysicalMemory 获取物内存理信息
                       SELECT* FROM Win32_LogicalDisk 获取磁盘信息
                       SELECT* FROM Win32_NetworkAdapter 获取网络适配器信息
                       SELECT* FROM Win32_NetworkProtocol 获取网络协议信息
                       SELECT* FROM Win32_Promter 获取打印和传真信息
                       SELECT* FROM Win32_Keyboard 获取键盘信息
                       SELECT* FROM Win32_PointingDevice 获取鼠标信息
                       SELECT* FROM Win32_SerialPort 获取串口信息
                       SELECT* FROM Win32_IDEController 获取IDE控制器信息
                       SELECT* FROM Win32_FloppyController 获取软体控制器信息
                       SELECT* FROM Win32_USBController 获取USB信息
                       SELECT* FROM Win32_SCSIController 获取SCSI信息
                       SELECT* FROM Win32_1394Controller 获取1394信息
                       SELECT* FROM Win32_PnPEntity 获取即插即用信息
           */

           foreach (ManagementObject CPUinfo in driveID.Get())
           {
               Console.WriteLine($"CPU制造商: {CPUinfo["manufacturer"]}");
               Console.WriteLine($"CPU名称: {CPUinfo["Name"]}");
           }
      

 

posted @ 2024-04-03 21:58  edolf  阅读(141)  评论(0编辑  收藏  举报