public void Backup(string dateTimeForBackup) { const int numOfSymbolAfterFirstBracket = 1; // number of symbol after first bracket in backup file name const int numOfSymbolBeforeLastBracketNum = 19; // number of symbol before last bracket in backup file name const int lastBracketNum = 21; // last bracket number in backup file name WorkWithConsoleClass workWithConsoleClass = new WorkWithConsoleClass(); DirectoryInfo backupFolderDirectoryInfo = new DirectoryInfo(Directory.GetCurrentDirectory() + "/Backups"); FileInfo[] allBackupTextFiles = backupFolderDirectoryInfo.GetFiles("*.txt", SearchOption.AllDirectories); DirectoryInfo textFilesDir = new DirectoryInfo(Directory.GetCurrentDirectory() + "/WatchedFolder"); FileInfo[] allTextFiles = textFilesDir.GetFiles("*.txt", SearchOption.AllDirectories); foreach (var file in allTextFiles) { File.Delete(file.FullName); } foreach (var backupFile in allBackupTextFiles) { if ((backupFile.Name.Substring(numOfSymbolAfterFirstBracket, numOfSymbolBeforeLastBracketNum) == dateTimeForBackup)) { File.Copy(backupFile.FullName, textFilesDir + "/" + backupFile.Name.Substring(lastBracketNum), true); workWithConsoleClass.PrintInfoAboutBackupedFile(textFilesDir + backupFile.Name, dateTimeForBackup); } } }
public static void Run() { using (FileSystemWatcher watcher = new FileSystemWatcher()) { watcher.Path = Directory.GetCurrentDirectory() + "/WatchedFolder"; watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; watcher.Filter = "*.txt"; watcher.IncludeSubdirectories = true; watcher.Changed += OnChanged; watcher.Created += OnChanged; watcher.Deleted += OnChanged; watcher.Renamed += OnRenamed; watcher.EnableRaisingEvents = true; WorkWithConsoleClass.ExitWatcherMode(); } }
static void Main(string[] args) { FileEditingClass.CopyAllFilesToBackups(); WorkWithConsoleClass.ConsoleInterface(); }