Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GET USB unique ID on putting or removing the key

I need to get the unique USB ID (not a Volume serial number ) on putting/removing the USB. But in all case "PNPDeviceID" is always empty. The code which I used is:

static void Main(string[] args)
{ 
    const string QUERY = @"select * from __InstanceOperationEvent within 1 where TargetInstance isa 'Win32_LogicalDisk' and (TargetInstance.DriveType=2)"; 
    Program p = new Program(); 
    ManagementEventWatcher w = new ManagementEventWatcher(new WqlEventQuery(QUERY));
    w.EventArrived += new EventArrivedEventHandler(p.OnWMIEvent); 
    w.Start();
    Console.ReadKey();
    w.Stop();
}

public void OnWMIEvent(object sender, EventArrivedEventArgs e)
{
    PropertyData p = e.NewEvent.Properties["TargetInstance"]; 
    if (p != null) 
    {
        ManagementBaseObject mbo = p.Value as ManagementBaseObject;
        PropertyData deviceid = mbo.Properties["DeviceID"]; 
        PropertyData drivetype = mbo.Properties["DriveType"];
        PropertyData driveSerial = mbo.Properties["VolumeSerialNumber"];
        PropertyData driveGUID = mbo.Properties["PNPDeviceID"];

        Console.WriteLine("{0}-{1}", "DeviceID",deviceid.Value); 
        Console.WriteLine("{0}-{1}", "DriveType",drivetype.Value);
        Console.WriteLine("{0}-{1}", "DriveSerial", driveSerial.Value);
        Console.WriteLine("{0}-{1}", "driveGUID", driveGUID.Value);
        Console.WriteLine();
    }
} 

Source is:this

I Can get the UNIQUE USB id with the following code:

ManagementObjectSearcher theSearcher = 
    new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive WHERE InterfaceType='USB'");

foreach(ManagementObject currentObject in theSearcher.Get())
{
    Console.WriteLine("{0}-{1}", "PNPDeviceID", currentObject.Properties["PNPDeviceID"].Value);            
}

So please can you tell me how can I combine them to receive PNPDeviceId (USB GUID) when I put/remove the USB stick

like image 638
Tiho Avatar asked Jun 07 '11 12:06

Tiho


People also ask

Is the USB Instance ID on Windows unique for a device?

is the USB Instance ID on Windows unique for a device? Excerpt from the Microsoft Device Instance ID page: A device instance ID is a system-supplied device identification string that uniquely identifies a device in the system.

What is device unique identifier (DUID)?

Device Unique Identifiers (DUIDs) Because techniques for uniquely identifying devices often become obsolete as technology evolves, Microsoft has developed a device ID format called the device unique ID (DUID) that is extensible and that can incorporate new techniques to identify devices as they become available.

How are USB device identifiers generated?

Since the Windows operating system generates special USB identifiers for printer and mass storage devices, the following documentation divides USB identifiers into two groups: For all USB devices, the USB bus driver generates a standard set of identifiers composed of values retrieved from the USB device and interface descriptors.

What are USB device-specific registry entries?

This topic describes USB device-specific registry entries. DevCon (Devcon.exe), the Device Console, is a command-line tool that displays detailed information about devices on computers running Windows. A hardware ID is a vendor-defined identification string that Windows uses to match a device to an INF file.


1 Answers

If you query directly the Win32_LogicalDisk class and you don't get values for the PNPDeviceID property, neither you will get values when you use this class inside of the wmi event. instead you can use the Win32_DiskDrive class with the __InstanceOperationEvent intrinsic event.

Select * From __InstanceOperationEvent Within 1 Where TargetInstance ISA 'Win32_DiskDrive' and TargetInstance.InterfaceType='USB'
like image 97
RRUZ Avatar answered Sep 22 '22 12:09

RRUZ