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