鉴于微软deprecated Transactional NTFS (TxF):替代使用事务NTFS
Microsoft强烈建议开发利用替代手段来实现应用程序的需要。许多TxF开发的场景可以通过更简单和更容易获得的技术来实现。此外,TxF可能在未来版本的Microsoft Windows中不可用。
虽然TxF是一组功能强大的API,但是自从Windows Vista开始,由于其复杂性和开发人员在应用程序开发中需要考虑的各种细微差别,开发人员对此API平台的兴趣非常有限。因此,微软正考虑在未来的Windows版本中摒弃TxF API,将开发和维护工作集中在其他功能和API上,这些功能和API对于大多数客户来说具有更大的价值。
这意味着,我需要一种替代方案:
我的交易需求是相当简单 - 移动两个文件:
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);
}
我想过到CopyFile
+ DeleteFile
转向MoveFile
:
CopyFile(testResults, testResultsArchive); //throws if there's a problem
CopyFile(cdcResponse, cdcResponseArchive); //throws if there's a problem
DeleteFile(testResults);
DeleteFile(cdcResponse);
,但我希望有一个良好的解决,而不是车的解决方案。所以,我再试一次:
CopyFile(testResults, testResultsArchive); //throws if there's a problem
CopyFile(cdcResponse, 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);
}
除了我希望的是好的解决方案,而不是马车之一。
我在源头上偷看。 “回滚”仍然归结为“File.Delete(path);';遇到问题可能无法删除文件。 *“真正的”*解决方案涉及使用'CreateFile',并重新实现'CopyFile',以便[可追溯地指示句柄“在句柄关闭时删除文件”](http://stackoverflow.com/问题/ 14572272 /如何对联合国的CreateFile)。但是,如果与事务完整性相关的问题不是最重要的问题(我正在使用.NET),那么我会使用类似的东西。公认。 –