public static bool MoveWithProgress(string source, string destination, Action <TransferProgress> progress) { var startTimestamp = DateTime.Now; NativeMethods.CopyProgressRoutine lpProgressRoutine = (size, transferred, streamSize, bytesTransferred, number, reason, file, destinationFile, data) => { TransferProgress fileProgress = new TransferProgress(startTimestamp, bytesTransferred) { Total = size, Transferred = transferred, StreamSize = streamSize, BytesTransferred = bytesTransferred, ProcessedFile = source }; try { progress(fileProgress); return(NativeMethods.CopyProgressResult.PROGRESS_CONTINUE); } catch (Exception ex) { return(NativeMethods.CopyProgressResult.PROGRESS_STOP); } }; if (!NativeMethods.MoveFileWithProgress(source, destination, lpProgressRoutine, IntPtr.Zero, NativeMethods.MoveFileFlags.MOVE_FILE_REPLACE_EXISTSING | NativeMethods.MoveFileFlags.MOVE_FILE_COPY_ALLOWED | NativeMethods.MoveFileFlags.MOVE_FILE_WRITE_THROUGH)) { return(false); } return(true); }
private static TransferResult CopyFileWithProgress(string sourceFile, string newFile, Action <TransferProgress> progress, CancellationToken cancellationToken) { int pbCancel = 0; var startTimestamp = DateTime.Now; NativeMethods.CopyProgressRoutine lpProgressRoutine = (size, transferred, streamSize, bytesTransferred, number, reason, file, destinationFile, data) => { TransferProgress fileProgress = new TransferProgress(startTimestamp, bytesTransferred) { Total = size, Transferred = transferred, StreamSize = streamSize, ProcessedFile = sourceFile }; try { lock (progress) { progress(fileProgress); return(NativeMethods.CopyProgressResult.PROGRESS_CONTINUE); } } catch (Exception ex) { return(NativeMethods.CopyProgressResult.PROGRESS_STOP); } }; if (cancellationToken.IsCancellationRequested) { return(TransferResult.Cancelled); } var ctr = cancellationToken.Register(() => pbCancel = 1); var result = NativeMethods.CopyFileEx(sourceFile, newFile, lpProgressRoutine, IntPtr.Zero, ref pbCancel, NativeMethods.CopyFileFlags.COPY_FILE_RESTARTABLE); if (cancellationToken.IsCancellationRequested) { return(TransferResult.Cancelled); } return(result ? TransferResult.Success : TransferResult.Failed); }
private static TransferResult CopyDirectoryWithProgress(string sourceDirectory, string destinationDirectory, Action <TransferProgress> progress, bool continueOnFailure, CancellationToken cancellationToken, bool copyContentOfDirectory) { var rootSource = new DirectoryInfo(sourceDirectory.TrimEnd('\\')); var rootSourceLength = rootSource.FullName.Length; var rootSourceSize = Helpers.DirSize(rootSource); long totalTransfered = 0; try { var destinationNewRootDir = new DirectoryInfo(destinationDirectory.TrimEnd('\\')); if (!copyContentOfDirectory) { destinationNewRootDir = Directory.CreateDirectory(Path.Combine(destinationDirectory, rootSource.Name)); } var fileCopyStartTimestamp = DateTime.Now; bool flag = false; foreach (var directory in rootSource.EnumerateDirectories("*", SearchOption.AllDirectories)) { flag = true; if (cancellationToken.IsCancellationRequested) { return(TransferResult.Cancelled); } var newName = directory.FullName.Substring(rootSourceLength + 1); Directory.CreateDirectory(Path.Combine(destinationNewRootDir.FullName, newName)); } foreach (var file in rootSource.EnumerateFiles("*", SearchOption.AllDirectories)) { flag = true; if (cancellationToken.IsCancellationRequested) { return(TransferResult.Cancelled); } var newName = file.FullName.Substring(rootSourceLength + 1); var result = CopyFileWithProgress(file.FullName, Path.Combine(destinationNewRootDir.FullName, newName), (partialProgress) => { var totalProgress = new TransferProgress(fileCopyStartTimestamp, partialProgress.BytesTransferred) { Total = rootSourceSize.Size, Transferred = totalTransfered + partialProgress.Transferred, BytesTransferred = totalTransfered + partialProgress.Transferred, StreamSize = rootSourceSize.Size, ProcessedFile = file.FullName }; progress(totalProgress); }, cancellationToken); if (result == TransferResult.Failed && !continueOnFailure) { return(TransferResult.Failed); } else if (result == TransferResult.Cancelled) { return(TransferResult.Cancelled); } totalTransfered += file.Length; } if (flag == false) { progress(new TransferProgress(fileCopyStartTimestamp, 1) { Total = 1 }); } } catch (Exception ex) { return(TransferResult.Failed); } return(TransferResult.Success); }