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); } }
internal static extern SafeFileHandle CreateFile( [In] string lpFileName, [In] NativeMethods.FileAccess dwDesiredAccess, [In] NativeMethods.FileShare dwShareMode, [In] IntPtr lpSecurityAttributes, [In] NativeMethods.FileMode dwCreationDisposition, [In] int dwFlagsAndAttributes, [In] IntPtr hTemplateFile);
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 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); } } }