public async Task CopyFileAsync(FileInfo source, FileInfo destination, CopyFileOptions options, CancellationToken cancellationToken) { if (source == null) { throw new ArgumentNullException(nameof(source)); } if (destination == null) { throw new ArgumentNullException(nameof(destination)); } if ((options & ~CopyFileOptions.All) != 0) // if any bit in options is set which is not defined by CopyFileOptions { throw new ArgumentOutOfRangeException(nameof(options)); } new FileIOPermission(FileIOPermissionAccess.Read, source.FullName).Demand(); new FileIOPermission(FileIOPermissionAccess.Write, destination.FullName).Demand(); CopyProgressRoutine cpr; if (CopyFileProgressChanged != null) { var fileEventArgs = new CopyFileEventArgs(source, destination); cpr = (totalFileSize, totalBytesTransferred, streamSize, streamBytesTransferred, streamNumber, callbackReason, sourceFile, destinationFile, data) => { try { cancellationToken.ThrowIfCancellationRequested(); var copyFileEventArgs = fileEventArgs; copyFileEventArgs.TotalFileSize = totalFileSize; copyFileEventArgs.TotalBytesTransferred = totalBytesTransferred; OnCopyFileProgressChanged(copyFileEventArgs); return((int)fileEventArgs.CallbackAction); } catch (OperationCanceledException) { return((int)CopyFileCallbackAction.Cancel); } }; } else { cpr = null; } var cancel = false; var copyFileExResult = await Task.Run(() => CopyFileEx(source.FullName, destination.FullName, cpr, IntPtr.Zero, ref cancel, (int)options), cancellationToken).ConfigureAwait(false); if (!copyFileExResult) { throw new IOException(new Win32Exception().Message); } }
private void OnCopyFileProgressChanged(CopyFileEventArgs e) { CopyFileProgressChanged?.Invoke(this, e); }