C# 列出windows上所有连接的设备
如何使用控制面板\硬件和声音\设备和打印机中的c#获取所有连接的设备C# 列出windows上所有连接的设备,c#,windows,controlpanel,input-devices,C#,Windows,Controlpanel,Input Devices,如何使用控制面板\硬件和声音\设备和打印机中的c#获取所有连接的设备 尝试在项目中添加对System.Management的引用,然后您可以进行如下试验: namespace ConsoleApplication1 { using System; using System.Collections.Generic; using System.Management; // need to add System.Management to your project refere
尝试在项目中添加对System.Management的引用,然后您可以进行如下试验:
namespace ConsoleApplication1 {
using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.
class Program {
static void Main(string[] args) {
var usbDevices = GetUSBDevices();
foreach(var usbDevice in usbDevices) {
Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}", usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
}
Console.Read();
}
static List < USBDeviceInfo > GetUSBDevices() {
List < USBDeviceInfo > devices = new List < USBDeviceInfo > ();
ManagementObjectCollection collection;
using(var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach(var device in collection) {
devices.Add(new USBDeviceInfo((string) device.GetPropertyValue("DeviceID"), (string) device.GetPropertyValue("PNPDeviceID"), (string) device.GetPropertyValue("Description")));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo {
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description) {
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID {
get;
private set;
}
public string PnpDeviceID {
get;
private set;
}
public string Description {
get;
private set;
}
}
}
尝试在项目中添加对System.Management的引用,然后您可以进行如下实验:
namespace ConsoleApplication1 {
using System;
using System.Collections.Generic;
using System.Management; // need to add System.Management to your project references.
class Program {
static void Main(string[] args) {
var usbDevices = GetUSBDevices();
foreach(var usbDevice in usbDevices) {
Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}", usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
}
Console.Read();
}
static List < USBDeviceInfo > GetUSBDevices() {
List < USBDeviceInfo > devices = new List < USBDeviceInfo > ();
ManagementObjectCollection collection;
using(var searcher = new ManagementObjectSearcher(@"Select * From Win32_USBHub"))
collection = searcher.Get();
foreach(var device in collection) {
devices.Add(new USBDeviceInfo((string) device.GetPropertyValue("DeviceID"), (string) device.GetPropertyValue("PNPDeviceID"), (string) device.GetPropertyValue("Description")));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo {
public USBDeviceInfo(string deviceID, string pnpDeviceID, string description) {
this.DeviceID = deviceID;
this.PnpDeviceID = pnpDeviceID;
this.Description = description;
}
public string DeviceID {
get;
private set;
}
public string PnpDeviceID {
get;
private set;
}
public string Description {
get;
private set;
}
}
}
谢谢这部分是有效的,我怎样才能得到控制面板中指定的模型名称?模型?就像在设备模型中一样?我试过了,不幸的是输出与控制面板中的显示方式不同,知道如何获得显示名称吗?谢谢。这部分是有效的,我怎样才能得到控制面板中指定的模型名称?模型?就像在设备模型中一样?我试过这个,不幸的是输出与控制面板中的显示方式不同,知道如何获得显示名称吗?