public static bool Move(string sourceFileName, string destFileName)
        {
            using (TransactionScope scope = new TransactionScope())
                using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle())
                {
                    // Allow copying to different volumes and to replace existing files
                    NativeMethods.MoveFileFlags moveFlags =
                        NativeMethods.MoveFileFlags.MOVEFILE_COPY_ALLOWED |
                        NativeMethods.MoveFileFlags.MOVEFILE_REPLACE_EXISTING;

                    bool status = NativeMethods.MoveFileTransacted(
                        sourceFileName,
                        destFileName,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        moveFlags,
                        ktmTx);

                    if (!status)
                    {
                        NativeMethods.HandleCOMError(Marshal.GetLastWin32Error());
                    }

                    scope.Complete();
                    return(status);
                }
        }
示例#2
0
        public static void Move(string existingFileName, string newFileName)
        {
            using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle())
            {
                NativeMethods.MoveFileFlags moveFlags = NativeMethods.MoveFileFlags.MOVEFILE_COPY_ALLOWED | NativeMethods.MoveFileFlags.MOVEFILE_REPLACE_EXISTING;

                bool result = NativeMethods.MoveFileTransacted(existingFileName, newFileName, IntPtr.Zero, IntPtr.Zero, moveFlags, ktmTx);

                // Throw an exception if an error occured.
                if (result == false)
                {
                    result = NativeMethods.MoveFileEx(existingFileName, newFileName, moveFlags);

                    if (result == false)
                    {
                        NativeMethods.HandleCOMError(Marshal.GetLastWin32Error());
                    }
                }
            }
        }
示例#3
0
 public static extern bool MoveFileWithProgress(
     string lpExistingFileName,
     string lpNewFileName,
     NativeMethods.CopyProgressRoutine lpProgressRoutine,
     IntPtr lpData,
     NativeMethods.MoveFileFlags dwCopyFlags);