Tutorial :File System Transactions using .NET on WinXP


I'm using WinXP on clients and Win2003 on a server.

I need do atomic actions: create-moving files, insert-update database.

Are there any good practices for file system transactions using WinXP? I know in Vista/Win2008/Win7 there are TxF transaction (NTFS) but not in WinXP.

I don't want use COM+, neither other complex solutions. Only need good sample code, for good practices.

Transactions and file-actions by Alberto Poblacion

In versions of Windows earlier than Vista, the filesystem is not transactional, so you need a separate tool to do transactions on your files.

You could use Component Services (COM+) to implement a Compensating Resource Manager (CRM). The CRM will provide the transaction log and will roll back changes during a system restart if it crashed during the update of your files, but you will have to provide the code (in your own DLL) to commit and rollback the transation, typically by means of moving files in and out of a temp folder. It can all be done in .Net by means of the System.EnterpriseServices namespace. If I recall correctly, Microsoft Official Course 2557 contains a chapter that teaches how to create a CRM, and the example that they use is built precisely on changes to the filesystem.

In newer versions of Windows, you can do transactional operations on NTFS:












You could create your own class that implements IEnlistmentNotification.

Here's an example of someone that did: http://www.chinhdo.com/20080825/transactional-file-manager/


You won't get true filesystem transactions for NTFS in XP. That said, you may not need it.

For example, with software installation, you can largely get the semantics of transactions for free by using something like Windows Installer.

What is it you are looking to ultimately accomplish?


The .NET Transactional File Manager should work under XP.

Out of interest, TxF is likely to be pulled from future releases of Windows (certain features have been deprecated in Win 8).

See How to write a transaction to cover Moving a file and Inserting record in database?.


You might want to look at the File.Replace method which I think uses transactions since it requires NTFS. http://msdn.microsoft.com/en-us/library/9d9h163f(v=vs.100).aspx

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »