Victor B.
Lake Forest, CA
Dec 26, 2012
93
Posts
Re: Checking Device Manager
Hi Steve,
You should use the System.Management assembly to detect system changes such as a USB insertion and after an event is detected, you can read the plug and play registry to see if your target device is installed. I have attached an example of this.
One important section of the example that I would like to point out is the AddHandlerUsbInsert() procedure. When we create a WqlEventQuery() and set the properties to define what type of event we are trying to trigger on. Specifically, we are looking for a "__InstanceCreationEvent" when a device is plugged in. If we were looking to trigger when a device is removed, we would use "__InstanceDeletionEvent". Also, we only want to trigger on USB devices, so we set a Condition that the event has to be a 'Win32_USBControllerdevice' rather than 'Win32_PnPEntity' that would cover all plug and play devices.
obQuery = new WqlEventQuery()
obQuery.EventClassName = "__InstanceCreationEvent"
obQuery.WithinInterval = new TimeSpan(0, 0, 3)
obQuery.Condition = "TargetInstance ISA 'Win32_USBControllerdevice'"
Regards,
Victor Brode
File Attachment:
UsbDetection.zip