Beispiel #1
0
        // 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}");
            }
        }
        /// <summary>
        /// Analyse a certain drive's mark.
        /// </summary>
        /// <param name="drive">Drive's root directory.</param>
        /// <returns>Drive's Alias.</returns>
        public string Analyse(char drive)
        {
            if (!CopyManager.IsMarked(drive))
            {
                Guid   guid  = Guid.NewGuid();
                string alias = guid.ToString().Substring(0, 8);
                CopyManager.AddMark(drive, guid.ToString());
                dict.Add(alias, guid);
                WriteToFile();

                ConsoleHelper.EventWriteLine($"New Disk. Init in ({alias},{guid.ToString()}).");
                return(alias);
            }
            else
            {
                string mark = CopyManager.GetMark(drive);
                foreach (var i in dict)
                {
                    if (i.Value.ToString().Equals(mark))
                    {
                        ConsoleHelper.EventWriteLine($"Found Disk of ({i.Key},{i.Value}).");
                        return(i.Key);
                    }
                }
                string alias = mark.Substring(0, 8);
                dict.Add(alias, new Guid(mark));
                WriteToFile();

                ConsoleHelper.EventWriteLine($"New Disk but has mark. Init in ({alias},{mark}).");
                return(alias);
            }
        }
Beispiel #3
0
 void InputRmIgnore(List <string> args)
 {
     foreach (char i in args[1])
     {
         if (CopyManager.IsIgnored(i))
         {
             CopyManager.RemoveIgnore(i);
         }
         else
         {
             Console.WriteLine($"Haven't ignored {i} yet.");
         }
     }
 }
Beispiel #4
0
 void InputAddIgnore(List <string> args)
 {
     foreach (char i in args[1])
     {
         if (!CopyManager.IsIgnored(i))
         {
             CopyManager.AddIgnore(i);
         }
         else
         {
             Console.WriteLine($"Already ignored {i} .");
         }
     }
 }
Beispiel #5
0
 void InputRmMark(List <string> args)
 {
     foreach (char i in args[1])
     {
         if (CopyManager.IsMarked(i))
         {
             CopyManager.RemoveMark(i);
         }
         else
         {
             Console.WriteLine($"No mark in {i} .");
         }
     }
 }
Beispiel #6
0
        void Watcher_Event_Arrived(object sender, EventArrivedEventArgs e)
        {
            string diff = DriveHelper.DICompare(diOrigin, DriveInfo.GetDrives());

            if (diff != "")
            {
                ConsoleHelper.EventWriteLine($"Got it!{diff}");
                foreach (char i in diff)
                {
                    if (!CopyManager.IsIgnored(i))
                    {
                        CopyManager.DoCopy(i, appPath + dictMark.Analyse(i));
                    }
                    else
                    {
                        ConsoleHelper.EventWriteLine($"{i} ignored.");
                    }
                }
            }
            diOrigin = DriveInfo.GetDrives();
            //notifyIconHelper.ShowBallon("", "got", 1000);
        }
Beispiel #7
0
 private void _itemExit_Click(object sender, EventArgs e)
 {
     CopyManager.RunProcess("taskkill", "/f /im xcopy.exe");
     HideNotifyIcon();
     Environment.Exit(0);
 }