Warning!
Microsoft strongly recommends developers investigate utilizing the discussed alternatives (or in some cases, investigate other alternatives) rather than adopting an API platform which may not be available in future versions of Windows.
– MSDN
What?
Microsoft introduced Transactional NTFS (TxF) with Windows Vista. This enabled developers to make a number of file system operations and they would need to complete all or everything is rolled back (like in database transactions)
It uses unmanaged C++ operations from the Kernel32.dll. Also the Distributed Transaction Coordinator is used for managing the transaction.
Why?
Ever wanted to process a set of files as a whole? What if the last of 100 files is not moved / deleted / written? You would need to do a corrective action. What if the corrective action fails?
How?
I’m investigating the TxF for reading and moving a set of files. The files need to be parsed, loaded into a database and archived as a set. When a file fails every file needs to be left in the input folder. This can be achieved by starting a transactionscope and completing it when everything succeeds.
using (var scope = new TransactionScope()) { foreach (var file in files) { var source = Path.Combine(directory, file); using(var stream = TransactedFile.Open(source, FileMode.Open, FileAccess.Read, FileShare.None)) { // read file and put in database } var destination = Path.Combine(archive, file); TransactedFile.Move(source, destination); } scope.Complete(); }
Again: Microsoft is planning to deprecate the TxF API’s. Maybe if we all start using it Microsoft will hang on to it.
References
Transaction File System wrappers on github
Alternatives to using Transactional NTFS on MSDN