C# How to trigger an event when a docked usb is detected?

585 Views Asked by At

Currently I have MannagementEventWatchers for when there is an arrival or removal of a device, this part works. However I want to detect when a device is inserted already using the same ManagementEventWatcher.

Currently I have:

var deviceArrivalQuery = new WqlEventQuery(String.Format("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = {0}", (int)DeviceEventType.Insertion));
var deviceRemovalQuery = new WqlEventQuery(String.Format("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = {0}", (int)DeviceEventType.Removal));
var devicePresentQuery = new WqlEventQuery(String.Format("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = {0}", (int)DeviceEventType.NoChange));

arrival = new ManagementEventWatcher(deviceArrivalQuery);
removal = new ManagementEventWatcher(deviceRemovalQuery);
present = new ManagementEventWatcher(devicePresentQuery);

arrival.EventArrived += (sender, args) => RaisePortsChangedIfNecessary(sender, DeviceEventType.Insertion);
removal.EventArrived += (sender, eventArgs) => RaisePortsChangedIfNecessary(sender, DeviceEventType.Removal);
present.EventArrived += (sender, args) => RaisePortsChangedIfNecessary(sender);

arrival.Start();
removal.Start();
present.Start();

I understand that 'EventArrived' might not work for my present port but I'm not quite sure what I need. Again, I just want it to detect when a device is already plugged in and have the same sender as the Arrival and Removals.

0

There are 0 best solutions below