TransactionScope With Files In C# [duplicate]

☆樱花仙子☆ 提交于 2020-01-09 04:50:27

问题


I've been using TransactionScope to work with the database and it feels nice. What I'm looking for is the following:

using(var scope=new TransactionScope())
{               
    // Do something with a few files...
    scope.Complete();
}

but obviously this doesn't work -- if there are 20 files, and an exception occurs on the 9th file, all previous 8 remain changed and the rest unchanged -- no rollback is performed. So, what would be the best way to implement a scope-like behavior for files?

I'm hoping there is a simple answer, but if not, could you just give me a few pointers, or point me to an related article?


回答1:


You're looking for Transactional NTFS, introduced by Windows Vista.

Here is a managed wrapper.




回答2:


You can try the .NET Transactional File Manager library available on Codeplex and NuGet. It supports any file system and is not a wrapper over Transactional NTFS.

From the project description:

Transactional File Manager is a .NET API 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 (works with System.Transactions.TransactionScope).



来源:https://stackoverflow.com/questions/2289395/transactionscope-with-files-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!