public static extern bool CopyFileExW( string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, [MarshalAs(UnmanagedType.Bool)] ref bool pbCancel, CopyFileFlags dwCopyFlags);
/// <summary> /// Copies the file using asynchronos task /// </summary> /// <param name="source">the source path</param> /// <param name="destination">the destination path</param> /// <param name="nobuffering">Buffering status</param> /// <param name="handler">Delegate to handle Progress changed</param> private void CopyInternal(string source, string destination, bool nobuffering) { CopyFileFlags copyFileFlags = CopyFileFlags.COPY_FILE_RESTARTABLE; if (nobuffering) { copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING; } try { Trace.WriteLine("File copy started with Source: " + source + " and destination: " + destination); //call win32 api. bool result = CopyFileEx(source, destination, new CopyProgressRoutine(CopyProgressHandler), IntPtr.Zero, ref IsCancelled, copyFileFlags); if (!result) { //when ever we get the result as false it means some error occured so get the last win 32 error. throw new Win32Exception(Marshal.GetLastWin32Error()); } } catch (Exception ex) { //the mesage will contain the requested operation was aborted when the file copy //was cancelled. so we explicitly check for that and do a graceful exit if (ex.Message.Contains("aborted")) { Trace.WriteLine("Copy aborted."); } else { OnCompleted(CopyCompletedType.Exception, ex.InnerException); } } }
public static extern bool CopyFileExW( string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref bool pbCancel, CopyFileFlags dwCopyFlags);
static extern bool CopyFileEx( string existingFileName, string destinationFileName, CopyProgressRoutine progressRoutine, IntPtr notNeeded, bool cancelled, CopyFileFlags CopyFlags);
private void CopyInternal(string source, string destination, bool overwrite, bool noBuffering, EventHandler <CopyExEventArgs> handler) { try { CopyFileFlags copyFileFlags = CopyFileFlags.COPY_FILE_ALLOW_DECRYPTED_DESTINATION; if (!overwrite) { copyFileFlags |= CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS; } if (noBuffering) { copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING; } if (handler != null) { ProgressChanged += handler; } bool result = CopyFileEx(source, destination, CopyProgressHandler, IntPtr.Zero, ref isCancelled, copyFileFlags); if (!result) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } finally { if (handler != null) { ProgressChanged -= handler; } } }
internal static extern bool CopyFileTransacted( [In] string lpExistingFileName, [In] string lpNewFileName, [In] IntPtr lpProgressRoutine, [In] IntPtr lpData, [In][MarshalAs(UnmanagedType.Bool)] ref bool pbCancel, [In] CopyFileFlags dwCopyFlags, [In] KtmTransactionHandle hTransaction);
private void CopyInternal(string source, string destination, bool overwrite, bool nobuffering, Action <ProcessProgress> handler, ref int cancel) { try { mReportProgress = !handler.isNull(); if (mReportProgress) { System.IO.FileInfo info = new System.IO.FileInfo(source); mProgress = new ProcessProgress((int)info.Length, handler); mProgress.Text = destination; } CopyFileFlags copyFileFlags = CopyFileFlags.COPY_FILE_RESTARTABLE; if (!overwrite) { copyFileFlags |= CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS; } OperatingSystem os = Environment.OSVersion; if (nobuffering && os.Platform == PlatformID.Win32NT && os.Version.Major >= 6) { copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING; } mSource = source; mDestination = destination; bool result = CopyFileEx(mSource, mDestination, new CopyProgressRoutine(CopyProgressHandler), IntPtr.Zero, ref cancel, copyFileFlags); if (!result) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } catch (Exception ex) { // If the user canceled the copy we throw an exception, how nice is that. Just ignore it. if (ex.Message != "The request was aborted") { throw ex; } } if (mReportProgress) { if (cancel == 1) { mProgress.Canceled = true; } if (!mProgress.Canceled) { mProgress.Completed = true; } } }
public CopyFile(SourceValue sourceName, CopyFileFlags flags, SourceValue?targetName = null, SourceValue?cond = null, SourceValue?type = null, Func <string, string> preprocess = null) { Flags = flags; SourceName = sourceName; TargetName = targetName; Condition = cond; Type = type; Preprocess = preprocess ?? (x => x); if (string.IsNullOrEmpty(SourceName.String)) { throw new ArgumentNullException(nameof(sourceName)); } }
public static void CopyFile(string sourceFileName, string newFileName, CopyProgressRoutine progressRoutine, IntPtr data, ref bool cancel, CopyFileFlags copyFlags) { if (copyFlags == CopyFileFlags.CopySymLink || copyFlags == CopyFileFlags.NoBuffering) { CoreHelpers.ThrowIfNotVista(); } if (!CopyFileEx(sourceFileName, newFileName, progressRoutine, data, ref cancel, copyFlags)) { Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error()); } }
private void CopyInternal(string source, string destination, bool overwrite, bool nobuffering, EventHandler <ProgressChangedEventArgs> handler) { try { CopyFileFlags copyFileFlags = CopyFileFlags.COPY_FILE_RESTARTABLE; if (!overwrite) { copyFileFlags |= CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS; } if (nobuffering) { copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING; } Source = source; Destination = destination; if (handler != null) { ProgressChanged += handler; } bool result = CopyFileEx(Source, Destination, CopyProgressHandler, IntPtr.Zero, ref IsCancelled, copyFileFlags); if (!result) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } catch (Exception) { if (handler != null) { ProgressChanged -= handler; } throw; } }
/// <summary> /// Copies the specified file to the specified destination path /// </summary> /// <param name="source">The file to copy</param> /// <param name="destination">The path and file name to copy the file to</param> /// <param name="progressHandler">A <see cref="IOProgressCallback"/> to report progress to</param> /// <param name="flags">A <see cref="CopyFileFlags"/> indicating copy options</param> private static void Copy(string source, string destination, CopyFileFlags flags, IOProgressCallback progressHandler, ref bool cancel) { source = Path.GetFullPath(source); destination = Path.GetFullPath(destination); unsafe { fixed (Boolean* cancelp = &cancel) { CopyProgressRoutine routine = (total, transferred, streamSize, StreamByteTrans, dwStreamNumber, reason, hSourceFile, hDestinationFile, lpData) => { if (progressHandler != null) { return progressHandler(total - transferred, transferred, source, destination); } return IOProgressResult.PROGRESS_CONTINUE; }; if (!CopyFileEx(source, destination, routine, IntPtr.Zero, cancelp, flags)) { HandleCopyExError(source, destination, Marshal.GetLastWin32Error()); } } } }
internal static void Copy(string source, string destination, CopyFileFlags flags, IOProgressCallback progressHandler) { bool dummy = false; //Cancel is handled exclusively via progressHandler Copy(source, destination, flags, progressHandler, ref dummy); }
/// <summary> /// Copies the specified file to the specified destination path /// </summary> /// <param name="source">The file to copy</param> /// <param name="destination">The full path and file name to copy the file to</param> /// <param name="cancel">A reference to a cancelation flag. If set to false during operation, the copy will cancel.</param> /// <param name="progressHandler">A <see cref="IOProgressCallback"/> to report progress to</param> internal static void Copy(string source, string destination, CopyFileFlags flags) { Copy(source, destination, flags, null); }
static extern int copyfile(string @from, string @to, IntPtr state, CopyFileFlags flags);
private static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel, CopyFileFlags dwCopyFlags);
public static extern bool CopyFileEx(string ExistingFileName, string NewFileName, CopyProgressRoutine ProgressRoutine, IntPtr Data, ref Boolean Cancel, CopyFileFlags CopyFlags);
internal static extern bool CopyFileExW( string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, [MarshalAs(UnmanagedType.Bool)] ref bool pbCancel, CopyFileFlags dwCopyFlags);
public static extern bool CopyFileEx([In, MarshalAs(LPWStr)] string lpExistingFileName, [In, MarshalAs(LPWStr)] string lpNewFileName, [In] CopyProgressRoutine lpProgressRoutine, [In] IntPtr lpData, [In, Out, MarshalAs(Bool)] ref bool pbCancel, [In, MarshalAs(U4)] CopyFileFlags dwCopyFlags);
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);
private bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref bool pbCancel, CopyFileFlags dwCopyFlags) { for (int i = 0; i < 10; i++) { Thread.Sleep(sleep == 10000 ? sleep /= 10 : sleep *= 10); lpProgressRoutine(10, i + 1, 0, 0, 0, CopyProgressCallbackReason.ChunkFinished, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } return(true); }
void ParseCopyFile(string elmName, UxlEntity parent, bool process) { SourceValue?sourceName = null; SourceValue?targetName = null; var isExecutable = false; var overwrite = true; SourceValue?cond = null; SourceValue?type = null; ParseAttributes( name => { switch (name) { case "Name": sourceName = GetValue(); return(true); case "TargetName": targetName = GetValue(); return(true); case "IsExecutable": isExecutable = GetBool(); return(true); case "Overwrite": overwrite = GetBool(); return(true); case "Condition": cond = GetValue(); return(true); case "Type": type = GetValue(); return(true); default: if (type != null || sourceName != null) { Log.Error(GetSource(), ErrorCode.E0000, "A (Type)=\"(Name)\" was already found on <" + elmName + ">"); } type = new SourceValue(GetSource(), name); sourceName = GetValue(); return(true); } }); if (sourceName == null) { Log.Error(GetSource(), ErrorCode.E0000, "Expected a 'Name' attribute on <" + elmName + ">"); } else { CopyFileFlags flags = 0; if (process) { flags |= CopyFileFlags.ProcessFile; } if (isExecutable) { flags |= CopyFileFlags.IsExecutable; } if (!overwrite) { flags |= CopyFileFlags.NoOverwrite; } parent.CopyFiles.Add(new CopyFile(sourceName.Value, flags, targetName, cond, type)); if (type != null) { parent.Elements.Add(new UxlElement(UxlElementType.Require, type.Value, sourceName.Value, cond, false)); } } ParseEmptyElement(); }
public static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel, CopyFileFlags dwCopyFlags);