/// <summary> /// Initializes a new instance of the <see cref="CopyFileProgress"/> class. /// </summary> /// <param name="sourceFilePath">The full path to the source file being copied.</param> /// <param name="destinationFilePath">The full path to the destination file being copied.</param> /// <param name="copyProgressCallback">A <see cref="CopyFileProgressCallback"/> method to call when progress is made.</param> /// <param name="userData">An object containing data to be used by the method.</param> public CopyFileProgress(string sourceFilePath, string destinationFilePath, CopyFileProgressCallback copyProgressCallback, object userData) { // Save the input parameters (Validation should be done by CopyFileEx since this is an internal constructor) SourceFilePath = sourceFilePath; DestinationFilePath = destinationFilePath; // It is OK for this to be null _progressCallback = copyProgressCallback; // Save the user's object for later user _userData = userData; }
public static void CopyFileEx(String source, String destination, CopyFileProgressCallback progressCallback) { CopyFileExWrapper.CopyFile( new FileInfo(source), new FileInfo(destination), CopyFileOptions.None, (src, dest, state, fileSize, bytesTransferred) => { progressCallback(src.FullName, dest.FullName, fileSize, bytesTransferred); return CopyFileCallbackAction.Continue; }); }
/// <summary> /// Initializes a new instance of the CopyFileProgress class. /// </summary> /// <param name="sourceFilePath">The full path to the source file being copied.</param> /// <param name="destinationFilePath">The full path to the destination file being copied.</param> /// <param name="copyProgressCallback">A <see cref="CopyFileProgressCallback"/> method to call when progress is made.</param> /// <param name="userData">An object containing data to be used by the method.</param> public CopyFileProgress(string sourceFilePath, string destinationFilePath, CopyFileProgressCallback copyProgressCallback, object userData) { // Save the input parameters (Validation should be done by CopyFileEx since this is an internal constructor) // SourceFilePath = sourceFilePath; DestinationFilePath = destinationFilePath; // It is OK for this to be null // _progressCallback = copyProgressCallback; // Save the user's object for later user // _userData = userData; }
public void CopyFileWithCallbackTest() { var stringBuilder = new StringBuilder(); CopyFileProgressCallback copyFileProgressCallback = delegate(CopyFileProgress progress, object userData) { _callbackCalled = true; ((StringBuilder)userData).Append(CallbackText); return(CopyFileProgressAction.Quiet); }; WimgApi.CopyFile(TestWimPath, _destinationPath, WimCopyFileOptions.None, copyFileProgressCallback, stringBuilder); _callbackCalled.ShouldBeTrue("The callback should have been called"); stringBuilder.ToString().ShouldBe(CallbackText); }
public static void CopyFileEx(String source, String destination, CancellationToken token, CopyFileProgressCallback progressCallback) { try { CopyFileExWrapper.CopyFile( new FileInfo(source), new FileInfo(destination), CopyFileOptions.None, (src, dest, state, fileSize, bytesTransferred) => { if (token.IsCancellationRequested) { return CopyFileCallbackAction.Cancel; } else { progressCallback(src.FullName, dest.FullName, fileSize, bytesTransferred); return CopyFileCallbackAction.Continue; } }); } catch (Exception ex) { //Converiosn to the OperationCancelledException; token.ThrowIfCancellationRequested(); throw; } }
/// <summary> /// Copies an existing file to a new file. Notifies the application of its progress through a callback function. If the source file has verification data, the contents of the file are verified during the copy operation. /// </summary> /// <param name="sourceFile">The name of an existing .wim file.</param> /// <param name="destinationFile">The name of the new file.</param> /// <param name="options">Specifies how the file is to be copied.</param> /// <param name="copyFileProgressCallback">A <see cref="CopyFileProgressCallback"/> method to call when progress is made copying the file and allowing the user to cancel the operation.</param> /// <param name="userData">An object containing data to be used by the progress callback method.</param> /// <exception cref="ArgumentNullException">sourceFile or destinationFile is null.</exception> /// <exception cref="Win32Exception">The Windows® Imaging API reported a failure.</exception> public static void CopyFile(string sourceFile, string destinationFile, WimCopyFileOptions options, CopyFileProgressCallback copyFileProgressCallback, object userData) { // See if sourceFile is null if (sourceFile == null) { throw new ArgumentNullException(nameof(sourceFile)); } // See if destinationFile is null if (destinationFile == null) { throw new ArgumentNullException(nameof(destinationFile)); } // Create a CopyFileProgress object CopyFileProgress fileInfoCopyProgress = new CopyFileProgress(sourceFile, destinationFile, copyFileProgressCallback, userData); // Cancel flag is always false bool cancel = false; // Call the native function if (!WimgApi.NativeMethods.WIMCopyFile(sourceFile, destinationFile, fileInfoCopyProgress.CopyProgressHandler, IntPtr.Zero, ref cancel, (DWORD)options)) { // Throw a Win32Exception based on the last error code throw new Win32Exception(); } }