[英]Identify USB flash drive in C#
我需要一个唯一标识 USB flash 驱动器的 ID。 该 ID 应该已经存在于 flash 驱动器上,因此它在我插入的每台 PC 上都是相同的 ID。看来 PNPDeviceID 不是唯一的。 谁能证明这一点? 如果它不是唯一的,是否有另一个 ID 来唯一标识 USB flash 驱动器?
我已经阅读了这些答案,但它们无法帮助我解决问题:
编辑:
这是我为获取 ID 而编写的代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
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; }
}
class Program
{
static List<string> Drives = new List<string>();
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;
}
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();
foreach (var usbDevice in usbDevices)
{
// USB-Massenspeichergerät means USB mass storage device in english
if (usbDevice.Description.Equals("USB-Massenspeichergerät"))
{
Console.WriteLine(usbDevice.PnpDeviceID);
}
}
Console.ReadKey();
}
}
}
这是我从同一供应商和完全相同类型的两个不同的 USB flash 驱动器获得的一些示例 ID(无论我采用 DeviceID 还是 PNPDeviceID,它们都是相同的):
如您所见,VID 和 PID 是等效的。 它们之间的唯一区别是“\”后面的数字。 正如我在PNPDeviceID Format的答案中读到的那样,它似乎只是系统创建的设备实例,用于识别 flash 驱动器。 因此,在阅读此答案后,我不确定那是否是唯一的。
这是一个英文版本的代码。
代码中已经提到了这一点。
我刚修好英语
请使用 nuget package 经理添加“System.Management”。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
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; }
}
class Program
{
static List<string> Drives = new List<string>();
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;
}
static void Main(string[] args)
{
var usbDevices = GetUSBDevices();
foreach (var usbDevice in usbDevices)
{
// USB-Massenspeichergerät means USB mass storage device in english
if (usbDevice.Description.Equals("USB Mass Storage Device"))
{
Console.WriteLine(usbDevice.PnpDeviceID);
}
}
Console.ReadKey();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.