加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

使用Transactional NTFS的替代方法

发布时间:2020-12-14 01:59:22 所属栏目:Windows 来源:网络整理
导读:鉴于微软有 deprecated Transactional NTFS (TxF): Microsoft strongly recommends developers utilize alternative means to achieve your application’s needs. Many scenarios that TxF was developed for can be achieved through simpler and more re
鉴于微软有 deprecated Transactional NTFS (TxF):

Microsoft strongly recommends developers utilize alternative means to achieve your application’s needs. Many scenarios that TxF was developed for can be achieved through simpler and more readily available techniques. Furthermore,TxF may not be available in future versions of Microsoft Windows.

While TxF is a powerful set of APIs,there has been extremely limited developer interest in this API platform since Windows Vista primarily due to its complexity and various nuances which developers need to consider as part of application development. As a result,Microsoft is considering deprecating TxF APIs in a future version of Windows to focus development and maintenance efforts on other features and APIs which have more value to a larger majority of customers.

这意味着我需要一个替代方案:

> CreateTransaction
> MoveFileTransacted
> CommitTransaction

我的交易要求相当简单 – 移动两个文件:

tx = BeginTransaction();
{
   MoveFile(testResults,testResultsArchive); //throws if there's a problem
   MoveFile(cdcResponse,cdcResponseArchive); //throws if there's a problem

   CommitTransaction(tx);
}
finally
{
    CloseHandle(tx);
}

我已经考虑过将MoveFile转换为CopyFile DeleteFile:

CopyFile(testResults,testResultsArchive); //throws if there's a problem
CopyFile(cdcResponse,cdcResponseArchive); //throws if there's a problem

DeleteFile(testResults);
DeleteFile(cdcResponse);

但我希望有一个好的解决方案,而不是一个错误的解决方案.所以我再试一次:

CopyFile(testResults,cdcResponseArchive); //throws if there's a problem

try
{
    DeleteFile(testResults);
}
catch (Exception e)
{
   DeleteFile(testResultsArchive);
   throw e;
}
try
{
    DeleteFile(cdcResponse);
}
catch (Exception e)
{
   DeleteFile(cdcResponseArchive);
}

除了我希望有一个好的解决方案,而不是一个有缺陷的解决方案.

解决方法

尝试 .NET Transactional File Manager.安全使用它相当简单.页面中的以下示例显示了该方法.它甚至看起来像作者是响应式的,并且能够使用新的有用功能扩展库.

// Wrap a file copy and a database insert in the same transaction
TxFileManager fileMgr = new TxFileManager();
using (TransactionScope scope1 = new TransactionScope())
{
    // Copy a file
    fileMgr.Copy(srcFileName,destFileName);

    // Insert a database record
    dbMgr.ExecuteNonQuery(insertSql);

    scope1.Complete();
}

如果您对自己的交易经理感兴趣,请务必查看this文章.如果仔细检查上面提到的库,你会发现它是以这种方式实现的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读