文件事物管理Transactional File Manager的实例详解


本文摘自PHP中文网,作者Y2J,侵删。

推荐一个文件事物管理 Transactional File Manager

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).

This library allows you to wrap file system operations in transactions like this:

1

2

3

4

5

// 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 Features

  • Support 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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

// 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的实例详解的详细内容!

相关阅读 >>

c#日期格式转换的公共方法类的实现详解

c#中foreach实例代码

c#如何实现添加和修改以及删除pdf书签的方法介绍

c#根据表格偶数与奇数加载不同颜色的实例分析

教你用c#检测含有中文字符串的实际长度

文件事物管理Transactional file manager的实例详解

.net core2.0小技巧之memorycache问题修复解决的方法(图)

c#如何实现json与对象之间互相转换功能示例

c# 常用函数总集

怎样用c#打印一个n行的菱形?实战详解

更多相关阅读请进入《Transactional》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...