在C#中识别USB闪存驱动器

4
我需要一个可以唯一标识USB闪存驱动器的ID。该ID应该已经存在于闪存驱动器上,以便在插入任何计算机时都是相同的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,它们都相同):

enter image description here

如您所见,VID和PID是等同的。它们之间唯一的区别在于反斜杠后面的数字。根据我在这里PNPDeviceID Format中阅读到的答案,它似乎只是由系统创建的设备实例,用于识别闪存驱动器。因此,在阅读了这个答案之后,我不确定它是否是唯一的。


你确定这个ID不是唯一的吗?我用WMI获取设备ID的方式,对我来说看起来是唯一的。 - Nitro.de
不,我不确定 :) 这就是我问的。根据我发布的答案,似乎它不是唯一的。我也尝试过使用WMI。我有两个不同类型的USB闪存驱动器,它们具有不同的PNPDeviceIDs,但我需要确定是否确实如此。 - L4c0573
2
Plug and Play(PnP)管理器为计算机中的每个设备生成一个实例标识符(ID)。每个实例ID对应于设备树中的单个设备节点,并唯一标识一个设备...也许这可以帮助您? - Nitro.de
2
我在这里找到了以下代码:Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification") - Nitro.de
你尝试过的代码在哪里?或者展示不同ID的代码在哪里?如果你把它发出来,我们也许能帮你找到问题所在。 - Ggalla1779
我在这里测试了一个类似的问题。 PNPDeviceID 明显不是唯一的。它取决于供应商/产品 ID、设备数量(具有相同的 VID/PID)和使用的 USB 端口。 在最坏的情况下,每个 USB 存储设备都没有唯一的编号。 - David Gausmann
1个回答

0

这里是英文版本的代码。

代码中已经提到过了。

我只是为英语语言进行了修复。

请使用NuGet包管理器添加“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();
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接