4

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):

enter image description here

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.

Community
  • 1
  • 1
L4c0573
  • 333
  • 1
  • 7
  • 23
  • are you sure the id isn't unique? Tried the wmi way to get device-ids and for me it seems to be unique. – Nitro.de Feb 25 '16 at 12:10
  • No I´m not sure :) Thats what I asked. It seems that it´s not unique thats what I read in the answers I posted. I also tried it via wmi. I had the two different usb flash drives of the same type, they had different PNPDeviceIDs, but I need to know I thats for sure. – L4c0573 Feb 25 '16 at 12:12
  • 2
    [the Plug and Play (PnP) manager generates an instance identifier (ID) for each device in the computer. Each instance ID corresponds to a single device node in the device tree and uniquely identifies a device...](https://msdn.microsoft.com/en-us/library/windows/hardware/ff552563(v=vs.85).aspx) maybe that helps? – Nitro.de Feb 25 '16 at 12:18
  • 2
    and i found this `Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.System.Profile.HardwareIdentification")` [here](http://stackoverflow.com/a/31803247/3932626) – Nitro.de Feb 25 '16 at 12:21
  • Where is the code you have tried? Or the code that shows the different IDs?. If you post it we may be able to help you spot the problem. – Ggalla1779 Feb 25 '16 at 14:38
  • I've tested it here for a similiar problem. The PNPDeviceID is definitively not unique. It depends on the vendor/product ID, the number of devices (with the same VID/PID) and the used USB port. In the worst case, there is no unique number for each USB storage device. – David Gausmann Sep 07 '16 at 15:24

1 Answers1

0

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();
        }
    }
}
haseakash
  • 31
  • 1
  • 6