Transactional File Manager is a .NET Standard 2.0 library that supports including file system operations such as file copy, move, delete, append, etc. in a transaction. It's an implementation of System.Transaction.IEnlistmentNotification.
This library allows you to wrap file system operations in transactions like this:
// Wrap a file copy and a database insert in the same transaction
IFileManager fm = new TxFileManager();
using (TransactionScope scope1 = new TransactionScope())
{
// Copy a file
fm.Copy(srcFileName, destFileName);
// Insert a database record
db.ExecuteNonQuery(insertSql);
scope1.Complete();
}
Support the following file operations in transactions:
- AppendAllText: Appends the specified string the file, creating the file if it doesn't already exist.
- Copy: Copy a file to another file.
- Delete: Delete a file.
- Move: Move a file.
- CreateDirectory: Create a directory.
- DeleteDirectory: Delete a directory.
- MoveDirectory: Move a directory.
- Snapshot: Take a snapshot of the specified file. The snapshot is used to rollback the file later if needed.
- WriteAllBytes: Write the specified bytes to the file.
- WriteAllText: Write the specified text content to the file.
If you have any suggestions for enhancements or bug reports please use the Issues list. Better yet, if possible join this project and contribute.
This library is available as a NuGet package.
This started out as a blog post. It was hosted on CodePlex and migrated to GitHub in 3/2020.
Additional contributors: @gvas, AirBreather.
- Add a reference to TxFileManager
dotnet add package TxFileManager
- Start writing code
IFileManager fm = new TxFileManager();
using (TransactionScope scope1 = new TransactionScope())
{
// Copy a file
fm.Copy(srcFileName, destFileName);
scope1.Complete();
}
The recommended method is to add a NuGet reference:
dotnet add package TxFileManager
Or Use Visual Studio's Manage NuGet Packages to add. Search for "TxFileManager".
It's not expensive to create new instances of TxFileManager as needed. There's a bit of overhead (like creating instances of any small class) but not much.
On the other hand, it's totally safe to re-use the same instance for multiple transactions, even nested transactions.
Yes - it's been tested for that.
Regardless of the specified IsolationLevel, the effective IsolationLevel is ReadUncommitted.
See Chinh's blog post: Include File Operations in Your Transactions Today with IEnlistmentNotification
By default, the path returned by Path.GetTempPath() is used to keep temporary files/directories used by TxFileManager. However, you can override that and have TxFileManager use another temp path:
IFileManager fm = new TxFileManager(myTempPath);
- Fork the repo
- Create a branch such as my-new-feature
- Make your change/fix and associated unit tests and commit
- Run the tests (
dotnet test
) - Open a Pull Request (PR) to the "master" branch
Notes: A release branch will be created out of the master branch when a release is made. The master branch will contain the latest features being added/tested for the next release.
- Add support for custom temp paths to address issues with file/dir operations accross filesystems
- Fix for resource leak in TxFileManager._txEnlistment
- Target .NET Standard 2.0
- Additional testing for .NET Core on Ubuntu
- Additional stress testing both on Windows and Ubuntu
- Created Github workflow to automatically build/test on ubuntu
- Added FxCop static analysis
- Bug fix Async code (Issue #27)
- Added new operations
- CopyDirectory
- MoveDirectory
- Added Encoding support to WriteAllText, AppendAllText
- Nuget package icon
- Misc refactors/clean-up
Please suggest your features using the Issues list.