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)); }
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()); } } } }
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()); } } } }