public static extern bool CopyFileEx( string lpExistingFileName, string lpNewFileName, APICopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 lpCancel, CopyFileOptions dwCopyFlags);
static private CopyFileResult CopyFileInternal( string sourceFilename, string destinationFilename, CopyProgressDelegate progressHandler, IntPtr userData, CopyFileOptions copyOptions, CancellationToken cancelToken) { // On error, throw IOException with the value from Marshal.GetLastWin32Error CopyProgressDelegate handler = progressHandler; int cancelFlag = 0; APICopyProgressRoutine callback; if (handler == null) { callback = null; } else { callback = new APICopyProgressRoutine((tfSize, xferBytes, strmSize, strmXferBytes, strmNum, cbReason, srcHandle, dstHandle, udata) => { var args = new CopyProgressArgs(tfSize, xferBytes, strmSize, strmXferBytes, strmNum, cbReason, srcHandle, dstHandle, udata); handler(args); return((Int32)args.Result); }); } if (cancelToken.CanBeCanceled) { cancelToken.Register(() => { cancelFlag = 1; }); } bool rslt = CopyFileEx( sourceFilename, destinationFilename, callback, userData, ref cancelFlag, copyOptions); int err = 0; if (!rslt) { err = Marshal.GetLastWin32Error(); } return(new CopyFileResult(rslt, err)); }