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;
int flags = 0;
if (!ReplaceExisting)
flags |= COPY_FILE_FAIL_IF_EXISTS;
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,
int dwCopyFlags);
private const int COPY_FILE_FAIL_IF_EXISTS = 0x0001;
private const int COPY_FILE_RESTARTABLE = 0x0002;
private const int COPY_FILE_OPEN_SOURCE_FOR_WRITE = 0x0004;
private const int COPY_FILE_ALLOW_DECRYPTED_DESTINATION = 0x0008;
private const int COPY_FILE_COPY_SYMLINK = 0x0800;
private const int COPY_FILE_NO_BUFFERING = 0x1000;
#endregion
}
}