using System; using System.Runtime.InteropServices; namespace Developpez.Dotnet.System { /// /// Représente une opération de copie de fichier qui peut signaler /// sa progression. /// public class FileCopyOperation : FileOperationBase { /// /// Initialise une nouvelle instance de FileCopyOperation /// /// Fichier source /// Fichier destination public FileCopyOperation(string source, string destination) : base(source, destination) { } /// /// Exécute la copie /// protected override void ExecuteCore() { bool cancel = false; var flags = default(CopyFileFlags); if (!ReplaceExisting) flags |= CopyFileFlags.FailIfExists; bool r = CopyFileEx( Source, Destination, ProgressCallback, IntPtr.Zero, ref cancel, flags); CheckError(r); } /// /// Renvoie FileOperationType.Copy /// public override FileOperationType OperationType { get { return FileOperationType.Copy; } } #region Interop declarations [DllImport("kernel32", SetLastError = true)] private static extern bool CopyFileEx( string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref bool pbCancel, CopyFileFlags dwCopyFlags); [Flags] private enum CopyFileFlags { // ReSharper disable UnusedMember.Local FailIfExists = 0x0001, Restartable = 0x0002, OpenSourceForWrite = 0x0004, AllowDecryptedDestination = 0x0008, CopySymlink = 0x0800, NoBuffering = 0x1000 // ReSharper restore UnusedMember.Local } #endregion } }