private void FileTransferBackgroundWorkerDoWork(object sender, DoWorkEventArgs e) { var fileTransferParameters = e.Argument as FileTransferParameters; if (fileTransferParameters == null) { return; } try { if (fileTransferParameters.SourceFilePath == fileTransferParameters.TargetFilePath) { return; } using (var source = fileTransferParameters.SourceDrive.OpenFileStreamForRead( fileTransferParameters.SourceFilePath)) { if (source == null) { throw new FileLoadException(); } var sourceLength = source.Length; using (var target = fileTransferParameters.TargetDrive.OpenFileStreamForWrite( fileTransferParameters.TargetFilePath)) { while (source.Position < sourceLength) { var bufferSize = 8192 < sourceLength - source.Position ? 8192 : Convert.ToInt32(sourceLength - source.Position); var buffer = new byte[bufferSize]; source.Read(buffer, 0, bufferSize); if (fileTransferBackgroundWorker.CancellationPending) { source.Close(); target.Close(); _targetDrive.DeleteFile(fileTransferParameters.TargetFilePath); return; } target.Write(buffer, 0, buffer.Length); if (fileTransferBackgroundWorker.CancellationPending) { source.Close(); target.Close(); _targetDrive.DeleteFile(fileTransferParameters.TargetFilePath); return; } var progress = Convert.ToDouble(source.Position) / Convert.ToDouble(sourceLength) * 100.0; fileTransferBackgroundWorker.ReportProgress(Convert.ToInt32(progress)); } } } if (_moveMode) { fileTransferParameters.SourceDrive.DeleteFile(fileTransferParameters.SourceFilePath); } } finally { fileTransferParameters.SourceDrive.DisposeFileStream(); fileTransferParameters.TargetDrive.DisposeFileStream(); } }