private async Task CopyFile( SourceComputer SourceComputer, string file, string destination) { var progressPart = new Progress<FileProgress>( p => { lock (SourceComputer.FileSize) { SourceComputer.FileSize[file] = p.Total; } lock (SourceComputer.FileProgress) { SourceComputer.FileProgress[file] = p.Transfered; } SourceComputer.JobStatus = JobStatus.Copying; if (p.Transfered >= p.Total) { SourceComputer.JobStatus = JobStatus.Success; } Dispatcher.InvokeAsync(RecomputeTotals); }); try { await FileEx.CopyAsync( file, destination, cancellationTokenSource.Token, progressPart); } catch (Win32Exception wex) { SourceComputer.JobStatus = JobStatus.Failed; SourceComputer.Exception = wex; } catch (OperationCanceledException) { if (SourceComputer.JobStatus == JobStatus.Copying || SourceComputer.JobStatus == JobStatus.Waiting) { SourceComputer.JobStatus = JobStatus.Cancelled; } throw; } }
private void EnumerateAndCopyFiles( SourceComputer computer, string location, string destination, string pattern, bool recurseIntoDirectories) { if (!location.EndsWith(Path.DirectorySeparatorChar + "")) location += Path.DirectorySeparatorChar; foreach ( var file in EnumerateFiles( location, pattern, recurseIntoDirectories ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)) { var path = Path.Combine(destination, computer.Name, file.Substring(location.Length)); var directoryInfo = new FileInfo(path).Directory; if (directoryInfo != null) Directory.CreateDirectory(directoryInfo.FullName); else throw new DirectoryNotFoundException($"Couldn't find directory for path: {path}"); #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed CopyFile( computer, file, path); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed } }