4

我需要一个唯一标识 USB 闪存驱动器的 ID。该 ID 应该已经存在于闪存驱动器上,因此它在我插入的每台 PC 上都是相同的 ID。似乎 PNPDeviceID 不是唯一的。谁能证明这一点?如果它不是唯一的,是否有另一个 ID 来唯一标识 USB 闪存驱动器?

我已经阅读了这些答案,但它们并不能帮助我解决问题:

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的答案中读到的,它似乎只是由系统创建的设备实例,用于识别闪存驱动器。因此,在阅读此答案后,我不确定那是否是独一无二的。

4

0 回答 0