简体   繁体   中英

Identify USB flash drive in C#

I need an ID that uniquely identifies an USB flash drive. 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. Can anyone proof that? And if its not unique, is there another ID to identify an USB flash drive uniquely?

I´ve already read those answers but they don´t help me to solve my problem:

PNPDeviceID Format

Is the PNPDeviceID unique

c#.NET USB device persistent identifier

EDIT:

Here´s what I´ve coded to get the 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):

在此处输入图像描述

As you can see the VID and the PID are equivalent. 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. 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.

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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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