0
我想要一个C#.NET中连接的USB设备列表。但我不明白该怎么做。如何使用C#.NET找到USB设备列表
我想要一个C#.NET中连接的USB设备列表。但我不明白该怎么做。如何使用C#.NET找到USB设备列表
到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}", usbDevice.DeviceID);
}
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")
));
}
collection.Dispose();
return devices;
}
}
class USBDeviceInfo
{
public USBDeviceInfo(string deviceID)
{
this.DeviceID = deviceID;
}
public string DeviceID { get; private set; }
}
}
希望它可以帮助你。
你甚至试图寻找解决方案吗?那么:http://stackoverflow.com/questions/3331043/get-list-of-connected-usb-devices只需在谷歌中键入您的问题... – bema 2013-05-06 09:53:18
我需要在列表框中显示它。我以前见过你的链接。但有些代码不起作用。 错误\t \t 1名称 'GetUSBDevices' 不存在于当前上下文\tģ存在:\ USB_Device_List \ USB_DEVICE \ USB_DEVICE \ Form1.cs中\t – 2013-05-06 10:08:01
USB_DEVICE @MarufHossain:所述GetUSBDevices()正下方的被定义主功能。 – 2013-05-06 10:20:03