C#获取本操作系统显卡,CPU,硬盘等相关信息,使用ManagementObjectSearcher对象来进行查询。
添加 引用 程序集 框架 System.Management
一、新建窗体应用程序DeviceManagerDemo。重命名Form1为FormDeviceManager。
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);
}
/// <summary>
/// 绑定硬件和操作系统 设备管理器
/// </summary>
private void BindAllComboBox()
{
List<Tuple<string, string>> listHandware = new List<Tuple<string, string>>();
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<Tuple<string, string>> listOperatingSystem = new List<Tuple<string, string>>();
listOperatingSystem.Add(new Tuple<string, string>(“Win32_TimeZone”, “时区”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_SystemDriver”, “驱动程序”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_DiskPartition”, “磁盘分区”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_LogicalDisk”, “逻辑磁盘”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_LogicalDiskToPartition”, “逻辑磁盘所在分区及始末位置”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_LogicalMemoryConfiguration”, “逻辑内存配置”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_PageFile”, “系统页文件信息”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_PageFileSetting”, “页文件设置”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_BootConfiguration”, “系统启动配置”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_ComputerSystem”, “计算机信息简要”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_OperatingSystem”, “操作系统信息”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_StartupCommand”, “系统自动启动程序”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_Service”, “系统安装的服务”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_Group”, “系统管理组”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_GroupUser”, “系统组帐号”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_UserAccount”, “用户帐号”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_Process”, “系统进程”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_Thread”, “系统线程”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_Share”, “共享”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_NetworkClient”, “已安装的网络客户端”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_NetworkProtocol”, “已安装的网络协议”));
listOperatingSystem.Add(new Tuple<string, string>(“Win32_PnPEntity”, “all device 【所有设备】”));
BindCombo(listOperatingSystem, cboOperatingSystem);
}
/// <summary>
/// 数据绑定组合下拉框
/// </summary>
/// <param name=”listSource”></param>
/// <param name=”cbo”></param>
private void BindCombo(List<Tuple<string, string>> listSource, ComboBox cbo)
{
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = listSource;
cbo.DataSource = bindingSource;
cbo.ValueMember = “Item1”;
cbo.DisplayMember = “Item2”;
}
/// <summary>
/// 显示某一个设备的相关信息
/// </summary>
/// <param name=”queryKey”>查询的关键字</param>
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<string> listQualifier = new List<string>();
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<string> listQualifier = new List<string>();
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();
}));
}
}
}
二、程序运行如图:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/39043.html