blah blah blah is here! blah blah » Close

up1down
link

I have a USB device that my program depends on. I'd like to be able to have an event fire when a USB device with a specific descriptor, VID or PID is plugged/unplugged. How can I achieve this?

last answered 2 years ago

1 answers

link

Check out my final post on this thread which shows how you can detect initially what USB devices are currently plugged in and also links to some more difficult code which shows how to detect such devices being added or removed in real time.

eeboy
499

Thanks!!! Exactly what I needed! I got both bits of code to work but I actually think the polling might be the best solution in my case (using the code you posted). I can actually boil everything down to this lone query... ManagementObjectSearcher searcher2 = new ManagementObjectSearcher("Select * from Win32_PnPEntity where Description like 'MyBoard%'"); Then check searcher2.Get().Count to see if it is greater than 1. Given this, do you see any issues with me polling this every 2 seconds or so? This would tell me if my device is connected or not and that's all I need to know.

vulpes
17279

The only reservation I have about polling at frequent intervals is that WMI can be a bit slow and, AFAIK, queries are not cached by the WMI infrastructure. But, other than that, I don't foresee any problems.

Feedback