Inheritance: Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
        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);
                }
        }
 internal static extern bool MoveFileTransacted(
     [In] string lpExistingFileName,
     [In] string lpNewFileName,
     [In] IntPtr lpProgressRoutine,
     [In] IntPtr lpData,
     [In] MoveFileFlags dwFlags,
     [In] KtmTransactionHandle hTransaction);
        public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
        {
            using (TransactionScope scope = new TransactionScope())
                using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle())
                {
                    NativeMethods.FileMode   internalMode   = TranslateFileMode(mode);
                    NativeMethods.FileShare  internalShare  = TranslateFileShare(share);
                    NativeMethods.FileAccess internalAccess = TranslateFileAccess(access);

                    SafeFileHandle hFile = NativeMethods.CreateFileTransacted(
                        path,
                        internalAccess,
                        internalShare,
                        IntPtr.Zero,
                        internalMode,
                        0,
                        IntPtr.Zero,
                        ktmTx,
                        IntPtr.Zero,
                        IntPtr.Zero);
                    if (hFile.IsInvalid)
                    {
                        NativeMethods.HandleCOMError(Marshal.GetLastWin32Error());
                    }

                    FileStream stream = new FileStream(hFile, access);
                    scope.Complete();

                    return(stream);
                }
        }
        public static bool Copy(string sourceFileName, string destFileName, bool overwrite)
        {
            using (TransactionScope scope = new TransactionScope())
                using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle())
                {
                    NativeMethods.CopyFileFlags copyFlags = NativeMethods.CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS;
                    if (overwrite)
                    {
                        copyFlags = 0; // TODO - Correctness - Which flag value is this really supposed to be? Works though...
                    }

                    bool pbCancel = false;
                    bool status   = NativeMethods.CopyFileTransacted(
                        sourceFileName,
                        destFileName,
                        IntPtr.Zero,
                        IntPtr.Zero,
                        ref pbCancel,
                        copyFlags,
                        ktmTx);

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

                    scope.Complete();
                    return(status);
                }
        }
        // TODO - BCL match - add overload that has a 'searchOption' argument
        public static string[] GetFiles(string path, string searchPattern)
        {
            using (TransactionScope scope = new TransactionScope())
                using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle())
                {
                    string dirSpec = System.IO.Path.Combine(path, searchPattern);

                    NativeMethods.WIN32_FIND_DATA findFileData;
                    SafeFileHandle hFind = FindFirstFileTransacted(dirSpec, ktmTx, out findFileData);
                    try
                    {
                        List <string> files = new List <string>();

                        // List all the other files in the directory.
                        do
                        {
                            files.Add(findFileData.cFileName);
                        }while (NativeMethods.FindNextFile(hFind, out findFileData));
                        int error = Marshal.GetLastWin32Error();

                        if (error != NativeMethods.ERROR_NO_MORE_FILES)
                        {
                            NativeMethods.HandleCOMError(error);
                        }

                        scope.Complete();
                        return(files.ToArray());
                    }
                    finally
                    {
                        // Ignore failures from this api just as the BCL does...
                        NativeMethods.FindClose(hFind);
                    }
                }
        }
 internal static extern SafeFileHandle FindFirstFileTransacted(
     [In] string lpDirSpec,
     [In] FINDEX_INFO_LEVELS fInfoLevelId,
     [Out] out WIN32_FIND_DATA lpFindFileData,
     [In] FINDEX_SEARCH_OPS fSearchOp,
     [In] IntPtr lpSearchFilter,
     [In] int dwAdditionalFlags,
     [In] KtmTransactionHandle hTransaction);
 internal static extern bool CopyFileTransacted(
     [In] string lpExistingFileName,
     [In] string lpNewFileName,
     [In] IntPtr lpProgressRoutine,
     [In] IntPtr lpData,
     [In][MarshalAs(UnmanagedType.Bool)] ref bool pbCancel,
     [In] CopyFileFlags dwCopyFlags,
     [In] KtmTransactionHandle hTransaction);
        public static KtmTransactionHandle CreateKtmTransactionHandle()
        {
            if (Transaction.Current == null)
            {
                throw new InvalidOperationException("Cannot create a KTM handle without Transaction.Current");
            }

            return(KtmTransactionHandle.CreateKtmTransactionHandle(Transaction.Current));
        }
 internal static extern SafeFileHandle CreateFileTransacted(
     [In] string lpFileName,
     [In] NativeMethods.FileAccess dwDesiredAccess,
     [In] NativeMethods.FileShare dwShareMode,
     [In] IntPtr lpSecurityAttributes,
     [In] NativeMethods.FileMode dwCreationDisposition,
     [In] int dwFlagsAndAttributes,
     [In] IntPtr hTemplateFile,
     [In] KtmTransactionHandle hTransaction,
     [In] IntPtr pusMiniVersion,
     [In] IntPtr pExtendedParameter);
        private static SafeFileHandle FindFirstFileTransacted(string dirSpec, KtmTransactionHandle ktmTx, out NativeMethods.WIN32_FIND_DATA findFileData)
        {
            SafeFileHandle hFile = NativeMethods.FindFirstFileTransacted(
                dirSpec,
                NativeMethods.FINDEX_INFO_LEVELS.FindExInfoStandard,
                out findFileData,
                NativeMethods.FINDEX_SEARCH_OPS.FindExSearchNameMatch,
                IntPtr.Zero,
                0,
                ktmTx);

            if (hFile.IsInvalid)
            {
                NativeMethods.HandleCOMError(Marshal.GetLastWin32Error());
            }

            return(hFile);
        }
        public static void Delete(string path)
        {
            using (TransactionScope scope = new TransactionScope())
                using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle())
                {
                    bool status = NativeMethods.DeleteFileTransacted(
                        path,
                        ktmTx);

                    if (!status)
                    {
                        // Match the BCL behavior and disregard non-existant files...
                        int error = Marshal.GetLastWin32Error();
                        if (error != NativeMethods.ERROR_FILE_NOT_FOUND)
                        {
                            NativeMethods.HandleCOMError(error);
                        }
                    }

                    scope.Complete();
                }
        }
 internal static extern bool DeleteFileTransacted(
     [In] string lpFileName,
     [In] KtmTransactionHandle hTransaction);
Example #13
0
        private static SafeFileHandle FindFirstFileTransacted(string dirSpec, KtmTransactionHandle ktmTx, out NativeMethods.WIN32_FIND_DATA findFileData)
        {
            SafeFileHandle hFile = NativeMethods.FindFirstFileTransacted(
                dirSpec,
                NativeMethods.FINDEX_INFO_LEVELS.FindExInfoStandard,
                out findFileData,
                NativeMethods.FINDEX_SEARCH_OPS.FindExSearchNameMatch,
                IntPtr.Zero,
                0,
                ktmTx);

            if (hFile.IsInvalid)
            {
                NativeMethods.HandleCOMError(Marshal.GetLastWin32Error());
            }

            return hFile;
        }