private void ListenUSBDisks() { try { OperatingSystem os = Environment.OSVersion; Version vs = os.Version; if (os.Platform == PlatformID.Win32NT && vs.Major == 5 && vs.Minor != 0) { //XP we need to poll ManagementEventWatcher watcher = new ManagementEventWatcher(); WqlEventQuery query = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_LogicalDisk'"); RemovableEventHandler handler = new RemovableEventHandler(); watcher.EventArrived += new EventArrivedEventHandler(handler.PollingArrived); watcher.Query = query; watcher.Start(); } else { //Vista or later no need to poll ManagementEventWatcher watcher = new ManagementEventWatcher(); WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2"); RemovableEventHandler handler = new RemovableEventHandler(); watcher.EventArrived += new EventArrivedEventHandler(handler.AsyncArrived); watcher.Query = query; watcher.Start(); } } catch (Exception e) { Program.form.ShowErrorDialog(e.ToString()); } }
private void ListenUSBDisks() { try { OperatingSystem os = Environment.OSVersion; Version vs = os.Version; if (os.Platform == PlatformID.Win32NT && vs.Major == 5 && vs.Minor != 0) { //XP we need to poll ManagementEventWatcher watcher = new ManagementEventWatcher(); WqlEventQuery query = new WqlEventQuery("SELECT * FROM __InstanceCreationEvent WITHIN 2 WHERE TargetInstance ISA 'Win32_LogicalDisk'"); RemovableEventHandler handler = new RemovableEventHandler(); watcher.EventArrived += new EventArrivedEventHandler(handler.PollingArrived); watcher.Query = query; watcher.Start(); } else { //Vista or later no need to poll ManagementEventWatcher watcher = new ManagementEventWatcher(); WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2"); RemovableEventHandler handler = new RemovableEventHandler(); watcher.EventArrived += new EventArrivedEventHandler(handler.AsyncArrived); watcher.Query = query; watcher.Start(); } } catch (Exception e) { Program.form.ShowErrorDialog(e.ToString()); } }