views:

24

answers:

0

Hi to all.

I'm trying to use function CopyFileEx from kernel32.dll in Windows 7 using .NET interop. MSDN says: If lpProgressRoutine returns PROGRESS_STOP due to the user stopping the operation, CopyFileEx will return zero and GetLastError will return ERROR_REQUEST_ABORTED. In this case, the partially copied destination file is left intact. On windows XP code works OK, but on Windows 7 partial file is being deleted automatically. Have I missed something?

using System;
using System.Runtime.InteropServices;

namespace Copy
{
    class Program
    {
        static void Main(string[] args)
        {
            var result = ExtendedCopy.XCopy("c:/original.rar", "c:/copy.rar");
        }
    }

    class ExtendedCopy
    {
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        private static extern bool CopyFileEx(string lpExistingFileName, string lpNewFileName, CopyProgressRoutine lpProgressRoutine, IntPtr lpData, ref Int32 pbCancel, CopyFileFlags dwCopyFlags);

        delegate CopyProgressResult CopyProgressRoutine(long TotalFileSize, long TotalBytesTransferred, long StreamSize, long StreamBytesTransferred, uint dwStreamNumber, CopyProgressCallbackReason dwCallbackReason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData);

        enum CopyProgressResult : uint
        {
            PROGRESS_CONTINUE = 0,
            PROGRESS_CANCEL = 1,
            PROGRESS_STOP = 2,
            PROGRESS_QUIET = 3
        }

        enum CopyProgressCallbackReason : uint
        {
            CALLBACK_CHUNK_FINISHED = 0x0,
            CALLBACK_STREAM_SWITCH = 0x1
        }

        [Flags]
        enum CopyFileFlags : uint
        {
            COPY_FILE_FAIL_IF_EXISTS = 0x1,
            COPY_FILE_RESTARTABLE = 0x2,
            COPY_FILE_OPEN_SOURCE_FOR_WRITE = 0x4,
            COPY_FILE_ALLOW_DECRYPTED_DESTINATION = 0x8
        }

        public static bool XCopy(String oldFile, String newFile)
        {
            int pbCancel = 0;
            return CopyFileEx(oldFile, newFile, new CopyProgressRoutine(CopyProgressHandler), IntPtr.Zero, ref pbCancel, CopyFileFlags.COPY_FILE_RESTARTABLE);
        }

        private static CopyProgressResult CopyProgressHandler(long total, long transferred, long streamSize, long StreamByteTrans, uint dwStreamNumber, CopyProgressCallbackReason reason, IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData)
        {
            if (transferred > 10000000)
            {
                return CopyProgressResult.PROGRESS_STOP;
            }
            return CopyProgressResult.PROGRESS_CONTINUE;
        }
    }
}