If you happen to be on a Windows Vista or Windows Server 2008 box, there is some goodness going your way.
There is a basic managed TxF (Transactional NTFS) wrapper available (unveiled by Jason Olson).
What this thing gives you is this:
try
{
using (TransactionScope tsFiles = new TransactionScope
TransactionScopeOption.RequiresNew))
{
WriteFile("TxFile1.txt");
throw new FileNotFoundException();
WriteFile("TxFile2.txt");
tsFiles.Complete();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
WriteFile method that does, well, file writing, is here:
using (TransactionScope tsFile = new TransactionScope
(TransactionScopeOption.Required))
{
Console.WriteLine("Creating transacted file '{0}'.", filename);
using (StreamWriter tFile = new StreamWriter(TransactedFile.Open(filename,
FileMode.Create,
FileAccess.Write,
FileShare.None)))
{
tFile.Write(String.Format("Random data. My filename is '{0}'.",
filename));
}
tsFile.Complete();
Console.WriteLine("File '{0}' written.", filename);
}
So we have a nested TransactionScope with a curious type - TransactedFile. Mind you, there is support for TransactedDirectory built in.
What's happening underneath is awesome. The wrapper talks to unmanaged implementation of TxF, which is built in on every Vista / Longhorn Server box.
What you get is transactional file system support with System.Transactions. And it's going to go far beyond that.
I wrote some sample code, go get it. Oh, BTW, remove the exception line to see the real benefit.
Download: Sample code
This sample is provided without any warranty. It's a sample, so don't use it in production environments.