public static void DeleteFile(string path) { // We do not need to do nothing if file not exist if (!File.Exists(path)) { return; } using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle()) { // Delete the transacted file using P/Invoke. bool result = NativeMethods.DeleteFileTransacted(path, ktmTx); // Throw an exception if an error occured. if (!result) { result = NativeMethods.DeleteFile(path); if (!result) { NativeMethods.HandleCOMError(Marshal.GetLastWin32Error()); } } } }
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);
public static void CreateDir( string path, ref List <string> createdDirs) { DirectoryInfo directoryInfo = new DirectoryInfo(path); using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle()) { CreateDirectory(directoryInfo, ktmTx, ref createdDirs); } }
public static SafeFileHandle Open( string path, FileMode mode, FileAccess access, FileShare share) { using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle()) { // Translate the managed flags to unmanaged flags. NativeMethods.FileMode internalMode = TranslateFileMode(mode); NativeMethods.FileShare internalShare = TranslateFileShare(share); NativeMethods.FileAccess internalAccess = TranslateFileAccess(access); // Create the transacted file using P/Invoke. SafeFileHandle hFile = NativeMethods.CreateFileTransacted( path, internalAccess, internalShare, IntPtr.Zero, internalMode, 0, IntPtr.Zero, ktmTx, IntPtr.Zero, IntPtr.Zero); { // Throw an exception if an error occured. if (hFile.IsInvalid) { hFile = NativeMethods.CreateFile( path, internalAccess, internalShare, IntPtr.Zero, internalMode, 0, IntPtr.Zero); if (hFile.IsInvalid) { NativeMethods.HandleCOMError(Marshal.GetLastWin32Error()); } } return(hFile); } } }
public static void DeleteDir(string path) { using (KtmTransactionHandle ktmTx = KtmTransactionHandle.CreateKtmTransactionHandle()) { bool result = NativeMethods.RemoveDirectoryTransacted(path, ktmTx); // Throw an exception if an error occured. if (!result) { result = NativeMethods.RemoveDirectory(path); if (!result) { NativeMethods.HandleCOMError(Marshal.GetLastWin32Error()); } } } }
private static void CreateDirectory( DirectoryInfo dirInfo, KtmTransactionHandle ktmTx, ref List <string> createdDirs) { if (createdDirs.Exists(dir => StringUtil.CompareIgnoreCase(dirInfo.FullName, dir))) { return; } if (dirInfo.Parent != null && !dirInfo.Parent.Exists) { CreateDirectory(dirInfo.Parent, ktmTx, ref createdDirs); } CreateDirectoryTransacted(dirInfo.FullName, ktmTx); createdDirs.Add(dirInfo.FullName); }
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()); } } } }
private static bool CreateDirectoryTransacted( string path, KtmTransactionHandle ktmTx) { bool result = NativeMethods.CreateDirectoryTransacted(null, path, IntPtr.Zero, ktmTx); if (!result) { result = NativeMethods.CreateDirectory(null, path, IntPtr.Zero); if (!result) { Log.WriteDebug("Failed to create directory : [{0}] ... possibly already exists. Error : [{1}]", path, Marshal.GetLastWin32Error().ToString()); } } return(result); }
internal static extern bool RemoveDirectoryTransacted( [In][Optional] string lpDirectory, [In] KtmTransactionHandle hTransaction);
internal static extern bool CreateDirectoryTransacted( [In][Optional] string lpTemplateDirectory, [In] string lpNewDirectory, [In][Optional] IntPtr lpSecurityAttributes, [In] KtmTransactionHandle hTransaction);
internal static extern bool MoveFileTransacted( [In] string lpExistingFileName, [In] string lpNewFileName, [In] IntPtr lpProgressRoutine, [In] IntPtr lpData, [In] MoveFileFlags dwFlags, [In] KtmTransactionHandle hTransaction);
internal static extern bool DeleteFileTransacted( [In] string lpFileName, [In] KtmTransactionHandle hTransaction);