internal static void UnzipArchiveWithReplace(string archiveName, string directoryName, string pathToArchive, string pathToLog) { string zipPath = pathToArchive + "\\" + archiveName; string extractPath = directoryName; string logPath = pathToLog; try { Directory.Delete(directoryName, true); } catch (Exception ex) { Console.WriteLine("Can't delete old directory: " + ex.Message); return; } try { ZipFile.ExtractToDirectory(zipPath, extractPath); string message = string.Format("Rollback to: {0}", archiveName); MyLogger.LogChanges(message, logPath); Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("All data has been successfully restored!"); Console.ResetColor(); } catch (Exception ex) { Console.WriteLine("Can't extract archive: " + ex.Message); } }
internal static void SaveChanges(string pathToDirectory, string pathToArchive, string pathToLog) { string startPath = pathToDirectory; string logPath = pathToLog; Guid g = Guid.NewGuid(); string archiveName = string.Format("{0}-{1}-{2}_{3}-{4}-{5}_{6}{7}", DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Day, DateTime.Now.Month, DateTime.Now.Year, g, ".zip"); string zipPath = pathToArchive; try { DirectoryInfo dir = new DirectoryInfo(zipPath); if (!dir.Exists) { dir.Create(); } } catch (Exception ex) { Console.WriteLine("Can't create archive folder: " + ex.Message); } try { ZipFile.CreateFromDirectory(startPath, zipPath + "\\" + archiveName); string message = string.Format("Archive created: {0}", archiveName); MyLogger.LogChanges(message, logPath); Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("The working directory was saved successfully in {0}!", zipPath); Console.ResetColor(); } catch (Exception ex) { Console.WriteLine("Can't zip files: " + ex.Message); } }
private void OnDeleted(object sender, FileSystemEventArgs e) { string message = string.Format("Delete file: '{0}'", e.FullPath); MyLogger.LogChanges(message, pathToLogFiles); changeCounter--; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(message); Console.ResetColor(); if (changeCounter == 0) { MyArchiver.SaveChanges(pathToDirectory, pathToArchive, pathToLogFiles); changeCounter = saveFrequency; } }
private void OnRenamed(object sender, RenamedEventArgs e) { string message = string.Format("Rename file: from '{0}' to '{1}'", e.OldFullPath, e.FullPath); MyLogger.LogChanges(message, pathToLogFiles); changeCounter--; Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(message); Console.ResetColor(); if (changeCounter == 0) { MyArchiver.SaveChanges(pathToDirectory, pathToArchive, pathToLogFiles); changeCounter = saveFrequency; } }