我需要一个可以唯一标识USB闪存驱动器的ID。该ID应该已经存在于闪存驱动器上,以便在插入任何计算机时都是相同的ID。似乎PNPDeviceID不是唯一的。有人能证明吗?如果它不是唯一的,是否有另一个ID可以唯一标识USB闪存驱动器?
我已经阅读了那些答案,但它们不能帮助我解决我的问题: PNPDeviceID格式 PNPDeviceID是否唯一 c#.NET USB设备持久标识符 编辑:
以下是我编写的代码来获取ID。
我已经阅读了那些答案,但它们不能帮助我解决我的问题: PNPDeviceID格式 PNPDeviceID是否唯一 c#.NET USB设备持久标识符 编辑:
以下是我编写的代码来获取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闪存驱动器中获取的示例ID(无论我使用DeviceID还是PNPDeviceID,它们都相同):
如您所见,VID和PID是等同的。它们之间唯一的区别在于反斜杠后面的数字。根据我在这里PNPDeviceID Format中阅读到的答案,它似乎只是由系统创建的设备实例,用于识别闪存驱动器。因此,在阅读了这个答案之后,我不确定它是否是唯一的。
Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification")
。 - Nitro.de