private void ReleaseAddInsetUSBHandler()
 {
     wAddedUSBHandler.Stop();
     wAddedUSBHandler.EventArrived -= new EventArrivedEventHandler(USBAdded);
     wAddedUSBHandler.Dispose();
     wAddedUSBHandler = null;
 }
 private void ReleaseAddRemoveUSBHandler()
 {
     wRemoveUSBHandler.Stop();
     wRemoveUSBHandler.EventArrived -= new EventArrivedEventHandler(USBRemoved);
     wRemoveUSBHandler.Dispose();
     wRemoveUSBHandler = null;
 }
        private void AddRemoveUSBHandler()
        {
            WqlEventQuery q;
            ManagementScope scope = new ManagementScope("root\\CIMV2");
            scope.Options.EnablePrivileges = true;

            try
            {
                q = new WqlEventQuery();
                q.EventClassName = "__InstanceDeletionEvent";
                q.WithinInterval = new TimeSpan(0, 0, 3);
                q.Condition = @"TargetInstance ISA 'Win32_USBControllerdevice'";
                wRemoveUSBHandler = new USBAddedOrRemovedEventWatcher( Parent, scope, q);
                wRemoveUSBHandler.EventArrived += new EventArrivedEventHandler(USBRemoved);
                wRemoveUSBHandler.Start();
            }

            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                if (wRemoveUSBHandler != null)
                    wRemoveUSBHandler.Stop();
            }
        }