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