public void DoBackup(string sourceFolder, string destFolder, bool doNotDeleteDestFiles) { var sourceNode = new FolderNode(new DirectoryInfo(sourceFolder)); var destNode = new FolderNode(new DirectoryInfo(destFolder)); var synchronizer = new Synchronizer(); var actions = synchronizer.Synchronize <FolderNode, FileNodeElement>(sourceNode, destNode); var fileSystemActions = new FileSystemActions(); foreach (var action in actions) { switch (action.ActionType) { case ActionType.DeleteDestNodeElement: if (!doNotDeleteDestFiles) { OnFileDelete?.Invoke(this, action.DestinationNodeElement); fileSystemActions.Remove(action.DestinationNodeElement); } break; case ActionType.CopySourceNodeElementToDestination: OnFileCopy?.Invoke(this, new FileCopyArgs(action.SourceNodeElement, action.DestinationNode)); fileSystemActions.Add(action.DestinationNode, action.SourceNodeElement); break; case ActionType.DeleteDestNode: if (!doNotDeleteDestFiles) { OnFolderDelete?.Invoke(this, action.DestinationNode); fileSystemActions.Remove(action.DestinationNode); } break; case ActionType.CopySourceNodeToDestination: OnFolderCopy?.Invoke(this, new FolderCopyArgs(action.SourceNode, action.DestinationNode)); fileSystemActions.Add(action.DestinationNode, action.SourceNode); break; } } }
private void OnChanged(object sender, FileSystemEventArgs e) { if (e.FullPath.Contains("~") && e.FullPath.Contains("TMP")) { return; } if (string.IsNullOrWhiteSpace(Path.GetFileName(e.FullPath))) { return; } switch (e.ChangeType) { case WatcherChangeTypes.Created: OnFileCreate?.Invoke(e); break; case WatcherChangeTypes.Deleted: OnFileDelete?.Invoke(e); break; case WatcherChangeTypes.Changed: OnFileChange?.Invoke(e); break; case WatcherChangeTypes.Renamed: break; case WatcherChangeTypes.All: break; default: return; } }