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?
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.
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.
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.
1 answers
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.
answered 2 years ago by:
17279
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.
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.