private static extern bool CopyFileEx( string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref bool pbCancel, int dwCopyFlags);
private static extern bool MoveFileWithProgress(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, MoveFileOptions dwCopyFlags);
/// <summary> /// Moves a file/folder asynchronously while invoking a Sytem.IProgress function to return a value between 0 and 100 (mostly used for Progressbars) /// </summary> /// <param name="sourceFileName">Full Path of the Source File/Folder</param> /// <param name="destFileName">Full Path of the Destination File/Folder</param> /// <param name="progress">IProgress callback function</param> /// <returns>Task=>bool</returns> public static Task <bool> MoveAsync(string sourceFileName, string destFileName, IProgress <double> progress) { CopyProgressRoutine copyProgressHandler = GetIProgressHandler(progress); return(MoveAsync(sourceFileName, destFileName, copyProgressHandler)); }
public static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel, CopyFileFlags dwCopyFlags);
private bool CopySystem(IVirtualItem sourceFile, IVirtualItem destFile) { long processed; object obj2; lock ((obj2 = this.FSnapshotLock)) { processed = this.FTotalProcessed.Processed; this.FCopyMode = CopyMode.System; } bool pbCancel = false; CopyProgressRoutine lpProgressRoutine = new CopyProgressRoutine(this.SystemCopyProgress); if (!System.IO.File.Exists(destFile.FullName)) { using (System.IO.File.Create(destFile.FullName)) { LocalFileSystemCreator.RaiseFileChangedEvent(WatcherChangeTypes.Created, destFile.FullName); } } IntPtr ptr = Marshal.AllocHGlobal(8); try { Marshal.WriteInt64(ptr, 0L); if (!Windows.CopyFileEx(sourceFile.FullName, destFile.FullName, lpProgressRoutine, ptr, ref pbCancel, (COPY_FILE) 0)) { lock ((obj2 = this.FSnapshotLock)) { this.FTotalProcessed.SetProcessedSize(processed); } int error = Marshal.GetLastWin32Error(); switch (error) { case 5: { Win32Exception inner = new Win32Exception(error); throw new UnauthorizedAccessException(inner.Message, inner); } case 0x57: { IVirtualFolder parent = destFile.Parent; if (parent != null) { throw new WarningException(string.Format(Resources.sNotEnoughSpaceInDest, parent.FullName, sourceFile.FullName)); } throw new Win32IOException(error); } case 0x4d3: return false; } throw new Win32IOException(error); } } finally { Marshal.FreeHGlobal(ptr); } LocalFileSystemCreator.RaiseFileChangedEvent(WatcherChangeTypes.Changed, destFile.FullName); return true; }
public static extern bool CopyFileEx(string existingFile, string newFile, CopyProgressRoutine routine, IntPtr data, IntPtr zero, uint flags);
private static unsafe extern bool CopyFileExW( string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, bool* pbCancel, uint dwCopyFlags );
public static extern Boolean MoveFileWithProgress([NotNull] String lpExistingFileName, [NotNull] String lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, MoveFileFlags dwCopyFlags);
public static extern Boolean CopyFileEx([NotNull] String lpExistingFileName, [NotNull] String lpNewFileName, [CanBeNull] CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 IsCancelled, MoveFileFlags dwCopyFlags);
private static extern bool MoveFileWithProgressA(string existingfile, string newfile, CopyProgressRoutine progressRoutine, IntPtr lpData, MoveFileFlags flags);
/// <summary> /// Copies a file/folder asynchronously which can be cancelled and exposes the CopyProgressRoutine for handling bytes transferred and other properties /// </summary> /// <param name="sourceFileName">Full Path of the Source File/Folder</param> /// <param name="destFileName">Full Path of the Destination File/Folder</param> /// <param name="token">Cancellation Token used to cancel the task</param> /// <param name="copyProgressHandler">CopyProgressRoutine callback function</param> /// <returns>Task=>bool</returns> public static Task <bool> CopyAsync(string sourceFileName, string destFileName, CancellationToken token, CopyProgressRoutine copyProgressHandler) { int pbCancel = 0; token.ThrowIfCancellationRequested(); var ctr = token.Register(() => pbCancel = 1); return(Task.Run(() => { try { return CopyFileEx(sourceFileName, destFileName, copyProgressHandler, IntPtr.Zero, ref pbCancel, CopyFileFlags.COPY_FILE_RESTARTABLE); } finally { ctr.Dispose(); } }, token)); }
/// <summary> /// Copies a file/folder asynchronously while invoking a Sytem.IProgress function to return a value between 0 and 100 and can be cancelled /// </summary> /// <param name="sourceFileName">Full Path of the Source File/Folder</param> /// <param name="destFileName">Full Path of the Destination File/Folder</param> /// <param name="token">Cancellation Token used to cancel the task</param> /// <param name="progress">IProgress callback function</param> /// <returns>Task=>bool</returns> public static Task <bool> CopyAsync(string sourceFileName, string destFileName, CancellationToken token, IProgress <double> progress) { CopyProgressRoutine copyProgressHandler = GetIProgressHandler(progress); return(CopyAsync(sourceFileName, destFileName, token, copyProgressHandler)); }
/// <summary> /// Moves a file/folder asynchronously which exposes the CopyProgressRoutine for handling bytes transferred and other properties /// </summary> /// <param name="sourceFileName">Full Path of the Source File/Folder</param> /// <param name="destFileName">Full Path of the Destination File/Folder</param> /// <param name="copyProgressHandler">CopyProgressRoutine callback function</param> /// <returns>Task=>bool</returns> public static Task <bool> MoveAsync(string sourceFileName, string destFileName, CopyProgressRoutine copyProgressHandler) { return(Task.Run(() => { return MoveFileWithProgress(sourceFileName, destFileName, copyProgressHandler, IntPtr.Zero, MoveFileFlags.MOVEFILE_COPY_ALLOWED | MoveFileFlags.MOVEFILE_REPLACE_EXISTING | MoveFileFlags.MOVEFILE_WRITE_THROUGH); })); }
internal static extern bool CopyFileExW( string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, [MarshalAs(UnmanagedType.Bool)] ref bool pbCancel, CopyFileFlags dwCopyFlags);
/// <summary> /// The Copy method for FileCopy class. /// </summary> /// <param name="oldFile">The old file.</param> /// <param name="newFile">The new file.</param> /// <param name="callback">The callback.</param> public static void Copy(string oldFile, string newFile, CopyProgressRoutine callback) { int pbCancel = 0; CopyFileEx(oldFile, newFile, callback, IntPtr.Zero, ref pbCancel, CopyFileFlags.COPY_FILE_RESTARTABLE); }
/// <summary> /// </summary> /// <param name="oldFile"></param> /// <param name="newFile"></param> /// <param name="progress"></param> /// <param name="isCancelled">I beleive this becomes 1 if the file copy was cancelled, and 0 otherwise.</param> public static void XCopy([NotNull] this String oldFile, [NotNull] String newFile, [CanBeNull] CopyProgressRoutine progress, ref Int32 isCancelled) { if (String.IsNullOrWhiteSpace(value: oldFile)) { throw new ArgumentException(message: "Value cannot be null or whitespace.", paramName: nameof(oldFile)); } if (String.IsNullOrWhiteSpace(value: newFile)) { throw new ArgumentException(message: "Value cannot be null or whitespace.", paramName: nameof(newFile)); } if (progress == default) { progress = CopyProgressHandler; } CopyFileEx(oldFile, newFile, progress, IntPtr.Zero, ref isCancelled, MoveFileFlags.MOVE_FILE_REPLACE_EXISTSING | MoveFileFlags.MOVE_FILE_WRITE_THROUGH | MoveFileFlags.MOVE_FILE_COPY_ALLOWED); }
internal static extern bool MoveFileWithProgress( string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, int dwFlags);
static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel, CopyFileFlags dwCopyFlags);
public static extern int CopyFileExW([MarshalAsAttribute(UnmanagedType.LPWStr)] string lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, IntPtr pbCancel, uint dwCopyFlags);
public static Task Copy(string source, string destination, OverwriteMode overwriteMode, CopyOptions options, CancellationToken?cancellationToken = null, Action <long, long> progressCallback = null) { if (string.IsNullOrEmpty(source)) { throw new ArgumentNullException("source"); } if (string.IsNullOrEmpty(destination)) { throw new ArgumentNullException("destination"); } var copyFileFlags = CopyFileFlags.COPY_FILE_RESTARTABLE; if (overwriteMode != OverwriteMode.AlwaysOverwrite) { copyFileFlags |= CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS; } if (options.HasFlag(CopyOptions.DisableBuffering)) { copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING; } int isCancelled = 0; var ct = cancellationToken ?? CancellationToken.None; CopyProgressRoutine progressRoutine = (total, transferred, streamSize, streamByteTrans, dwStreamNumber, reason, hSourceFile, hDestinationFile, lpData) => { if (progressCallback != null && reason == CopyProgressCallbackReason.CALLBACK_CHUNK_FINISHED) { progressCallback(transferred, total); } if (ct.IsCancellationRequested) { return(CopyProgressResult.PROGRESS_CANCEL); } else { return(CopyProgressResult.PROGRESS_CONTINUE); } }; return(Task.Run( () => { if (!CopyFileEx(source, destination, progressRoutine, IntPtr.Zero, ref isCancelled, copyFileFlags)) { int errorCode = Marshal.GetLastWin32Error(); // https://msdn.microsoft.com/en-us/library/cc231199.aspx // ERROR_FILE_EXISTS = 0x00000050; // ERROR_ALREADY_EXISTS = 0x000000B7 // ERROR_OBJECT_ALREADY_EXISTS = 0x00001392 // ERROR_OBJECT_NAME_EXISTS = 0x000002BA if (errorCode == 0x00000050 || errorCode == 0x000000B7 || errorCode == 0x00001392 || errorCode == 0x000002BA) { if (overwriteMode == OverwriteMode.OverwriteIfDifferent) { if (IOHelper.AreSameFile(source, destination)) { return; } else { copyFileFlags &= ~CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS; if (CopyFileEx(source, destination, progressRoutine, IntPtr.Zero, ref isCancelled, copyFileFlags)) { return; } } } } throw new Win32Exception(errorCode); } })); }
static unsafe extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, Boolean* pbCancel, CopyFileFlags dwCopyFlags);
static extern unsafe bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, Boolean *pbCancel, CopyFileFlags dwCopyFlags);
public static extern bool MoveFileWithProgress(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, MoveFileFlags dwFlags);
public static extern bool CopyFileExW([In][MarshalAsAttribute(UnmanagedType.LPWStr)] string lpExistingFileName, [In][MarshalAs(UnmanagedType.LPWStr)] string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, IntPtr pbCancel, uint dwCopyFlags);