0

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.

Aiden-B
  • 41
  • 5
  • 3
    [How do I get information about recently connected USB device?](https://stackoverflow.com/a/54298316/7444103) -- You can pair it with [Get the serial number of USB storage devices](https://stackoverflow.com/a/51806262/7444103) if you need to read detailed information on the structure of an USB storage device (not just the serial number :) – Jimi Jun 13 '22 at 20:06
  • Thank you, this was a step in the right direction for me! :) – Aiden-B Jun 14 '22 at 18:10

0 Answers0