public void Run() { wholeJob.ct.ThrowIfCancellationRequested(); wholeJob.Args.To.Refresh(); wholeJob.Args.From.Refresh(); bool destinationExisted = wholeJob.Args.To.Exists; try { if (destinationExisted) { throw new IOException("Destination directory already exists."); } else if (!wholeJob.Args.From.Exists) { throw new IOException("Source directory does not exist."); } var files = wholeJob.Args.From.GetFiles(); var dirs = wholeJob.Args.From.GetDirectories(); wholeJob.Args.To.Create(); progressCache = new float[files.Length + dirs.Length]; int cacheInd = 0; foreach (var file in files) { int cacheIndLocal = cacheInd++; wholeJob.ct.ThrowIfCancellationRequested(); var destFile = new FileInfo(Path.Combine(wholeJob.Args.To.FullName, file.Name)); var job = new FileTransferJob(new FileTransferArguments(file, destFile, wholeJob.Args.Settings), p => RegisterProgress(p, cacheIndLocal), wholeJob.ct); job.Run(); } foreach (var dir in dirs) { int cacheIndLocal = cacheInd++; wholeJob.ct.ThrowIfCancellationRequested(); var destDir = new DirectoryInfo(Path.Combine(wholeJob.Args.To.FullName, dir.Name)); var job = new DirectoryTransferJob(new DirectoryTransferArguments(dir, destDir, wholeJob.Args.Settings), p => RegisterProgress(p, cacheIndLocal), wholeJob.ct); job.Run(); } if (totalProgress < 100) { totalProgress = 100f; ProgressChange?.Invoke(100f); } } catch (Exception e) when(e is IOException | e is UnauthorizedAccessException | e is SecurityException | e is FileOperationException) { throw new DirectoryTransferException(wholeJob.Args, e); } finally { if (totalProgress < 100) { //Rollback wholeJob.Args.To.Refresh(); if (wholeJob.Args.To.Exists && !destinationExisted) { wholeJob.Args.To.Delete(true); } } } }
public DirectoryCopy(DirectoryTransferJob job) { wholeJob = job; }