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
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.
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.
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.
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.
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With