You need to use the new TxF, the Transacted NTFS introduced in Vista, Windows 7 and Windows Server 2008. This is a good introductory article: Enhance Your Apps With File System Transactions. It contains a small managed sample of enrolling a file operation into a system transaction:
// IKernelTransaction COM Interface
[Guid("79427A2B-F895-40e0-BE79-B57DC82ED231")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IKernelTransaction
{
int GetHandle(out IntPtr pHandle);
}
[DllImport(KERNEL32,
EntryPoint = "CreateFileTransacted",
CharSet = CharSet.Unicode, SetLastError = true)]
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);
....
using (TransactionScope scope = new TransactionScope())
{
// Grab Kernel level transaction handle
IDtcTransaction dtcTransaction =
TransactionInterop.GetDtcTransaction(managedTransaction);
IKernelTransaction ktmInterface = (IKernelTransaction)dtcTransaction;
IntPtr ktmTxHandle;
ktmInterface.GetHandle(out ktmTxHandle);
// Grab transacted file handle
SafeFileHandle hFile = NativeMethods.CreateFileTransacted(
path, internalAccess, internalShare, IntPtr.Zero,
internalMode, 0, IntPtr.Zero, ktmTxHandle,
IntPtr.Zero, IntPtr.Zero);
... // Work with file (e.g. passing hFile to StreamWriter constructor)
// Close handles
}
You'll need to enroll your SQL operation in the same transaction, which will occur automatically under a TransactionScope. But I highly recommend you override the default TransactionScope options to use ReadCommitted isolation level:
using (TransactionScope scope = new TransactionScope(
TransactionScope.Required,
new TransactionOptions
{ IsolationLevel = IsolationLEvel.ReadCommitted}))
{
...
}
W/o this you'll get the default Serializable isolation level which is way way overkill for most cases.