简体   繁体   English

识别USB flash驱动器在C#

[英]Identify USB flash drive in C#

I need an ID that uniquely identifies an USB flash drive.我需要一个唯一标识 USB flash 驱动器的 ID。 The ID should already exists on the flash drive, so that it is the same ID on every PC I plug it in. It seems that the PNPDeviceID is not unique.该 ID 应该已经存在于 flash 驱动器上,因此它在我插入的每台 PC 上都是相同的 ID。看来 PNPDeviceID 不是唯一的。 Can anyone proof that?谁能证明这一点? And if its not unique, is there another ID to identify an USB flash drive uniquely?如果它不是唯一的,是否有另一个 ID 来唯一标识 USB flash 驱动器?

I´ve already read those answers but they don´t help me to solve my problem:我已经阅读了这些答案,但它们无法帮助我解决问题:

PNPDeviceID Format PNPDeviceID格式

Is the PNPDeviceID unique PNPDeviceID 是否唯一

c#.NET USB device persistent identifier c#.NET USB 设备持久标识符

EDIT:编辑:

Here´s what I´ve coded to get the ID.这是我为获取 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();
            }
        }
    }

And this are some example IDs that I get from two different USB flash drives of the same vendor and exactly the same type (it doesn´t matter if I take the DeviceID or the PNPDeviceID, they are the same):这是我从同一供应商和完全相同类型的两个不同的 USB flash 驱动器获得的一些示例 ID(无论我采用 DeviceID 还是 PNPDeviceID,它们都是相同的):

在此处输入图像描述

As you can see the VID and the PID are equivalent.如您所见,VID 和 PID 是等效的。 The only difference between them is the number after the '\'.它们之间的唯一区别是“\”后面的数字。 As I read in the answer here PNPDeviceID Format , it seems to be only the device instance, that is created by the system to identify the flash drive.正如我在PNPDeviceID Format的答案中读到的那样,它似乎只是系统创建的设备实例,用于识别 flash 驱动器。 So after reading this answer I´m not sure if thats unique.因此,在阅读此答案后,我不确定那是否是唯一的。

Here is an english version code.这是一个英文版本的代码。

Already this was mentioned in code.代码中已经提到了这一点。

I just fixed for english language我刚修好英语

Kindly add "System.Management" using nuget package manager.请使用 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM