Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private const string deviceSelectWmiQuery = @"SELECT * FROM Win32_PnPEntity where DeviceID Like 'USB%' AND Name Like '%Apple Mobile Device USB%'";
- private const string deviceOperationWmiQuery = @"SELECT * FROM __InstanceOperationEvent WITHIN 3 WHERE TargetInstance ISA 'Win32_PnPEntity' AND TargetInstance.Name Like '%Apple Mobile Device USB%'"; // Win32_USBHub
- private ManagementEventWatcher usbDeviceWatcher;
- //...
- var usbDeviceOperationQuery = new WqlEventQuery(deviceOperationWmiQuery);
- var scope = new ManagementScope("root\\CIMV2");
- usbDeviceWatcher = new ManagementEventWatcher(scope, usbDeviceOperationQuery);
- public void WatchUsbDevices()
- {
- if (pollingTimer.IsEnabled)
- {
- StopWatchingUsbDevices();
- }
- usbDeviceWatcher.EventArrived += UsbDeviceOperationEvent;
- usbDeviceWatcher.Start();
- pollingTimer.Start();
- }
- public void StopWatchingUsbDevices()
- {
- usbDeviceWatcher.EventArrived -= UsbDeviceOperationEvent;
- usbDeviceWatcher?.Stop();
- pollingTimer?.Stop();
- }
- private void UsbDeviceOperationEvent(object sender, EventArrivedEventArgs e)
- {
- var usbDeviceInstance = e.NewEvent["TargetInstance"] as ManagementBaseObject;
- if (usbDeviceInstance != null)
- {
- if (e.NewEvent.ClassPath.ClassName.Equals("__InstanceCreationEvent"))
- {
- UsbDeviceInsertedEvent(sender, usbDeviceInstance);
- }
- else if(e.NewEvent.ClassPath.ClassName.Equals("__InstanceDeletionEvent"))
- {
- UsbDeviceRemovedEvent(sender, usbDeviceInstance);
- }
- }
- }
- private void UsbDeviceInsertedEvent(object sender, ManagementBaseObject usbDeviceInstance)
- {
- var deviceID = (usbDeviceInstance.GetPropertyValue("DeviceID") as string)?.ToLower();
- // targetObject.Dispatcher.Invoke(() => { targetDevices.Add(device); });
- }
- private void UsbDeviceRemovedEvent(object sender, ManagementBaseObject instance)
- {
- var deviceID = (instance.GetPropertyValue("DeviceID") as string)?.ToLower();
- // targetObject.Dispatcher.Invoke(() => targetDevices.Remove(deviceToRemove.First()));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement