// This part is referenced from Stack Overflow. Thanks user "learns CSharp"! void Watcher() { try { WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent"); ManagementEventWatcher watcher = new ManagementEventWatcher(query); watcher.EventArrived += new EventArrivedEventHandler(Watcher_Event_Arrived); diOrigin = DriveInfo.GetDrives(); watcher.Start(); Console.WriteLine("Watcher started. Waiting for event..."); // Start listening for events while (isWatching && (!isExit)) { Thread.Sleep(100); } watcher.Stop(); CopyManager.RunProcess("taskkill", "/f /im xcopy.exe"); Console.WriteLine("Watcher stopped."); } catch (ManagementException ex) { Console.WriteLine($"Error occured when trying to start drive watcher: {ex.Message}"); } }
private void _itemExit_Click(object sender, EventArgs e) { CopyManager.RunProcess("taskkill", "/f /im xcopy.exe"); HideNotifyIcon(); Environment.Exit(0); }