推荐一个文件事物管理 transactional file manager
project descriptiontransactional 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).
this library allows you to wrap file system operations in transactions like this:
// wrap a file copy and a database insert in the same transactiontxfilemanager filemgr = new txfilemanager();using (transactionscope scope1 = new transactionscope())
{// copy a file
filemgr.copy(srcfilename, destfilename);// insert a database record
dbmgr.executenonquery(insertsql);scope1.complete();
}
current
featuressupport the following file operations in transactions:
appendalltext
copy
createdirectory
deletedirectory
deletefile
move
snapshot
writealltext
writeallbytes
this library supports any file system and is not a wrapper over transactional ntfs (see alphafs).
examples// completely unrealistic example showing how various file operations, including operations done // by library/3rd party code, can participate in transactions.ifilemanager filemanager = new txfilemanager();using (transactionscope scope1 = new transactionscope())
{ filemanager.writealltext(infilename, xml); // snapshot allows any file operation to be part of our transaction.
// all we need to know is the file name.
//the statement below tells the txfilemanager to remember the state of this file.
// so even though xslcompiledtransform has no knowledge of our txfilemanager, the file it creates (outfilename)
// will still be restored to this state in the event of a rollback.
filemanager.snapshot(outfilename); xslcompiledtransform xsl = new xslcompiledtransform(true); xsl.load(uri); xsl.transform(infilename, outfilename); // write to database 1. this database op will get committed/rolled back along with the file operations we are doing in this transaction.
mydb1.executenonquery(sql1); // write to database 2. the transaction is promoted to a distributed transaction here.
mydb2.executenonquery(sql2); // let's delete some files
for (string filename in filestodelete)
{
filemanager.delete(filename);
} // just for kicks, let's start a new nested transaction. since we specify requiresnew here, this nested transaction
// will be committed/rolled back separately from the main transaction.
// note that we can still use the same filemanager instance. it knows how to sort things out correctly.
using (transactionscope scope2 = new transactionscope(transactionscopeoptions.requiresnew))
{ filemanager.movefile(anotherfile, anotherfiledest);
} // move some files
for (string filename in filestomove)
{
filemanager.move(filename, getnewfilename(filename));
} // finally, let's create a few temporary files...
// disk space has to be used for something.
// the nice thing about filemanager.gettempfilename is that
// the temp file will be cleaned up automatically for you when the transactionscope completes.
// no more worries about temp files that get left behind.
for (int i=0; i<10; i++)
{
filemanager.writealltext(filemanager.gettempfilename(), "testing 1 2");
} scope1.complete(); // in the event an exception occurs, everything done here will be rolled back including the output xsl file.}
这是一个开源项目。原始项目网站是 事务文件管理器。
版权所有(c)2008-2013 chinh do
特此授予任何获得本软件和相关文档文件(“软件”)副本的人免费许可,无限制地处理本软件,包括但不限于使用,复制,修改,合并的权利,发布,分发,再授权和/或出售本软件的副本,并允许提供本软件的人员遵守以下条件:
上述版权声明和本许可声明应包含在本软件的所有副本或主要部分。
该软件“按原样”提供,不附带任何明示或暗示的保证,包括但不限于适销性,适用于特定用途和不侵权的保证。在任何情况下,作者或版权所有者均不对任何索赔,损害或其他责任负责,无论是否因与本软件或本软件的使用或其他交易相关的任何合同,侵权行为或其他方面的行为软件。
以上就是文件事物管理transactional file manager的实例详解的详细内容。