C#获取本操作系统显卡,CPU,硬盘等相关信息,使用ManagementObjectSearcher对象来进行查询。
添加 引用 程序集 框架 System.Management
FormDeviceManager.cs相关源代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DeviceManagerDemo
{
public partial class FormDeviceManager : Form
{
public FormDeviceManager()
{
InitializeComponent();
//绑定设备管理器
BindAllComboBox();
//组合框选择索引的改变事件
cboHardware.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
cboOperatingSystem.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
}
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
if (cbo == null || cbo.SelectedIndex < 0)
{
return;
}
string queryKey = cbo.SelectedValue as string;
DisplayDeviceMessage(queryKey);
}
///
/// 绑定硬件和操作系统 设备管理器
///
private void BindAllComboBox()
{
List
listHandware.Add(Tuple.Create("Win32_Processor", "CPU中央处理器"));
listHandware.Add(Tuple.Create("Win32_PhysicalMemory", "物理内存条"));
listHandware.Add(Tuple.Create("Win32_Keyboard", "键盘"));
listHandware.Add(Tuple.Create("Win32_PointingDevice", "点输入设备,包括鼠标。"));
listHandware.Add(Tuple.Create("Win32_FloppyDrive", "软盘驱动器"));
listHandware.Add(Tuple.Create("Win32_DiskDrive", "硬盘驱动器"));
listHandware.Add(Tuple.Create("Win32_CDROMDrive", "光盘驱动器"));
listHandware.Add(Tuple.Create("Win32_BaseBoard", "主板"));
listHandware.Add(Tuple.Create("Win32_BIOS", "BIOS芯片"));
listHandware.Add(Tuple.Create("Win32_ParallelPort", "并口"));
listHandware.Add(Tuple.Create("Win32_SerialPort", "串口"));
listHandware.Add(Tuple.Create("Win32_SerialPortConfiguration", "串口配置"));
listHandware.Add(Tuple.Create("Win32_SoundDevice", "多媒体设备,一般指声卡"));
listHandware.Add(Tuple.Create("Win32_SystemSlot", "主板插槽 (ISA & PCI & AGP)"));
listHandware.Add(Tuple.Create("Win32_USBController", "USB控制器"));
listHandware.Add(Tuple.Create("Win32_NetworkAdapter", "网络适配器"));
listHandware.Add(Tuple.Create("Win32_NetworkAdapterConfiguration", "网络适配器设置"));
listHandware.Add(Tuple.Create("Win32_Printer", "打印机"));
listHandware.Add(Tuple.Create("Win32_PrinterConfiguration", "打印机设置"));
listHandware.Add(Tuple.Create("Win32_PrintJob", "打印机任务"));
listHandware.Add(Tuple.Create("Win32_TCPIPPrinterPort", "打印机端口"));
listHandware.Add(Tuple.Create("Win32_POTSModem", "MODEM"));
listHandware.Add(Tuple.Create("Win32_POTSModemToSerialPort", "MODEM端口"));
listHandware.Add(Tuple.Create("Win32_DesktopMonitor", "显示器"));
listHandware.Add(Tuple.Create("Win32_DisplayConfiguration", "显卡"));
listHandware.Add(Tuple.Create("Win32_DisplayControllerConfiguration", "显卡设置"));
listHandware.Add(Tuple.Create("Win32_VideoController", "显卡视频控制器"));
listHandware.Add(Tuple.Create("Win32_VideoSettings", "显卡支持的显示模式"));
BindCombo(listHandware, cboHardware);
List
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
listOperatingSystem.Add(new Tuple
BindCombo(listOperatingSystem, cboOperatingSystem);
}
///
/// 数据绑定组合下拉框
///
///
///
private void BindCombo(List
{
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = listSource;
cbo.DataSource = bindingSource;
cbo.ValueMember = "Item1";
cbo.DisplayMember = "Item2";
}
///
/// 显示某一个设备的相关信息
///
/// 查询的关键字
private void DisplayDeviceMessage(string queryKey)
{
rtxtDisplay.Clear();
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM " + queryKey);
ManagementObjectCollection managementObjectCollection = searcher.Get();
DisplayMessage($"【{queryKey}】设备信息个数:{managementObjectCollection.Count}");
foreach (ManagementObject managementObject in managementObjectCollection)
{
DisplayMessage($"管理对象的属性个数:{managementObject.Properties.Count}");
StringBuilder sb = new StringBuilder();
foreach (PropertyData propertyData in managementObject.Properties)
{
List
foreach (QualifierData qualifierData in propertyData.Qualifiers)
{
listQualifier.Add($"[{qualifierData.Name}:{qualifierData.Value}]");
}
sb.Append($" {propertyData.Name}:{propertyData.Value}--类型:【{propertyData.Type}】,限定符集合:【{string.Join(",", listQualifier)}】\n");
}
DisplayMessage(sb.ToString());
DisplayMessage($"管理对象的系统属性个数:{managementObject.SystemProperties.Count}");
sb = new StringBuilder();
foreach (PropertyData propertyData in managementObject.SystemProperties)
{
List
foreach (QualifierData qualifierData in propertyData.Qualifiers)
{
listQualifier.Add($"[{qualifierData.Name}:{qualifierData.Value}]");
}
sb.Append($" {propertyData.Name}:{propertyData.Value}--类型:【{propertyData.Type}】,限定符集合:【{string.Join(",", listQualifier)}】\n");
}
DisplayMessage(sb.ToString());
}
}
private void FormDeviceManager_Load(object sender, EventArgs e)
{
//添加 引用 程序集 框架 System.Management
ComboBox_SelectedIndexChanged(cboHardware, e);
}
private void DisplayMessage(string message)
{
this.BeginInvoke(new Action(() =>
{
rtxtDisplay.AppendText($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}-->{message}\n");
rtxtDisplay.ScrollToCaret();
}));
}
}
}