/// <summary> /// Backup the target path if needed. /// </summary> public void BackupIfNeeded(FileHashesSet fileHashes, BackupFolders backupFolders) { if (!File.Exists(targetPath)) { return; } var hashes = fileHashes.Get(Path.GetDirectoryName(targetPath)); string originalHash; if (hashes.TryGetHash(targetPath, out originalHash)) { // Compare with existing var currentHash = Hash(targetPath); if (currentHash == originalHash) { return; // No need to backup } } var backupFolder = backupFolders.Get(Path.GetDirectoryName(targetPath)); Directory.CreateDirectory(backupFolder); File.Copy(targetPath, Path.Combine(backupFolder, Path.GetFileName(targetPath))); }
/// <summary> /// Update the hash of the target path. /// </summary> public void UpdateHash(FileHashesSet fileHashes) { var hashes = fileHashes.Get(Path.GetDirectoryName(targetPath)); hashes.Add(targetPath, Hash(targetPath)); }
/// <summary> /// Backup the target path if needed. /// </summary> public void BackupIfNeeded(FileHashesSet fileHashes, BackupFolders backupFolders) { if (!File.Exists(targetPath)) return; var hashes = fileHashes.Get(Path.GetDirectoryName(targetPath)); string originalHash; if (hashes.TryGetHash(targetPath, out originalHash)) { // Compare with existing var currentHash = Hash(targetPath); if (currentHash == originalHash) return; // No need to backup } var backupFolder = backupFolders.Get(Path.GetDirectoryName(targetPath)); Directory.CreateDirectory(backupFolder); File.Copy(targetPath, Path.Combine(backupFolder, Path.GetFileName(targetPath))); }