public static void OnDirectoryDelete(object sender, FileSystemEventArgs e)
 {
     WatchingLogic.DirectoryDelete(e.FullPath);
 }
        public static void OnFileRename(object sender, RenamedEventArgs e)
        {
            WatchingLogic.Change(e.FullPath, WatcherChangeTypes.Created);

            WatchingLogic.Change(e.OldFullPath, WatcherChangeTypes.Deleted);
        }
 public static void OnDirectoryRename(object sender, RenamedEventArgs e)
 {
     WatchingLogic.RecursiveDirectoryRename(e.FullPath, e.OldFullPath);
 }
 public static void OnFileChange(object sender, FileSystemEventArgs e)
 {
     WatchingLogic.Change(e.FullPath, e.ChangeType);
 }
Beispiel #5
0
        private static void Main()
        {
            Console.WriteLine("Greetings! You are using Backup Program for .txt files.");
            Console.WriteLine();

            if (string.IsNullOrEmpty(Config.WatchingDirectory) ||
                string.IsNullOrEmpty(Config.BackupDirectory))
            {
                SetConfig();
            }

            while (true)
            {
                ShowMainMenu();
                string choice = MainMenuChoice();

                switch (choice)
                {
                case "1":
                    FileSystemWatcher fileWatcher = new FileSystemWatcher(Config.WatchingDirectory, "*.txt")
                    {
                        EnableRaisingEvents   = true,
                        IncludeSubdirectories = true,
                        NotifyFilter          = NotifyFilters.FileName | NotifyFilters.Size,
                    };

                    fileWatcher.Changed += WatcherEvents.OnFileChange;
                    fileWatcher.Created += WatcherEvents.OnFileChange;
                    fileWatcher.Deleted += WatcherEvents.OnFileChange;
                    fileWatcher.Renamed += WatcherEvents.OnFileRename;

                    FileSystemWatcher directoryWatcher = new FileSystemWatcher(Config.WatchingDirectory, "*.*")
                    {
                        EnableRaisingEvents   = true,
                        IncludeSubdirectories = true,
                        NotifyFilter          = NotifyFilters.DirectoryName,
                    };

                    directoryWatcher.Renamed += WatcherEvents.OnDirectoryRename;
                    directoryWatcher.Deleted += WatcherEvents.OnDirectoryDelete;

                    IEnumerable <FileInfo> files = WatchingLogic.GetAllFiles();
                    Config.CurrentFiles = WatchingLogic.GetCurrentFilesHashes(files);
                    WatchingLogic.MakeFullDirectoryCopy();

                    Console.WriteLine("The program is watching files in setted directory. Press Esc to stop it.");

                    while (Console.ReadKey().Key != ConsoleKey.Escape)
                    {
                        Thread.Yield();
                    }

                    fileWatcher.EnableRaisingEvents      = false;
                    directoryWatcher.EnableRaisingEvents = false;
                    break;

                case "2":
                    Console.WriteLine("Please enter the date and time to backup");
                    Console.Write("in format yyyy.MM.dd_HH-mm: ");

                    DateTime dateForBackUp;

                    try
                    {
                        dateForBackUp = BackupLogic.GetDateForBackup(Console.ReadLine());
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("Invalid date input.");
                        Console.WriteLine();
                        break;
                    }

                    BackupLogic.ClearDirectory(Config.WatchingDirectory);

                    try
                    {
                        BackupLogic.GetBackToChosenDate(dateForBackUp);
                    }
                    catch (DirectoryNotFoundException)
                    {
                        Console.WriteLine("Error. The backup directory was not found.");
                        Console.WriteLine("Choose 3rd Main menu item to set the path of observing directory.");
                        Console.WriteLine();
                        break;
                    }
                    catch (FileNotFoundException)
                    {
                        Console.WriteLine("Error. The log file was not found.");
                        Console.WriteLine();
                        break;
                    }

                    break;

                case "3":
                    Console.WriteLine("Press Enter if you want to clear the history and set new directory to watch.");
                    Console.WriteLine("Press any another key to get back to get back to Main menu.");

                    if (Console.ReadKey().Key == ConsoleKey.Enter)
                    {
                        try
                        {
                            BackupLogic.ClearDirectory(Config.BackupDirectory);
                        }
                        catch (DirectoryNotFoundException)
                        {
                            Console.WriteLine("Error. The backup directory was not found so there is nothing to clear.");
                            SetConfig();
                            break;
                        }

                        Console.WriteLine("All history was successfully cleared.");

                        SetConfig();
                    }

                    break;

                case "4":
                    PrintConfig();
                    break;

                case "5":
                    return;

                default:
                    Console.WriteLine("Wrong input.");
                    break;
                }
            }
        }