.NET下的原子文件复制 [英] Atomic file copy under .NET

查看:22
本文介绍了.NET下的原子文件复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用 System.IO.File.Copy(...) 函数复制文件的服务器应用程序.我的文件可能很大,因此,如果机器崩溃,它很有可能在复制过程中发生.

I am building a server app that copies files using System.IO.File.Copy(...) function. My files can be rather large, therefore, it has a fair chance that if the machine crashes, it happens during copying.

重新启动服务后,我应该可以拿起复制任务并继续.如何检测副本是否已成功完成或因服务器崩溃而中断?

After restarting the service, I should be able to pick up the copy tasks and continue. How can I detect if a copy has been successfully completed or interrupted by server crash?

我目前的计划是将文件复制到一个临时名称,并在复制完成后将其重命名为最终名称.这样文件命名就可以在崩溃时携带状态信息.

My current plan is to copy the files to a temporary name and once copying completed rename it to the final name. This way the file naming is able to carry the state information over the crash.

您有什么好的/更好的建议吗?

Do you have any good/better suggestions?

目标操作系统是 Win2003,因此事务性 NTFS 不可用

Target OS is Win2003, therefore transactional NTFS is not available

推荐答案

其他人建议使用事务性 NTFS,如果您在 Vista 或更高版本上进行部署,这很好.如果您需要支持 XP(或更早版本),那么临时文件后移动(重命名)是最好的解决方案.

Other have suggested transactional NTFS which is fine if you're deploying on Vista or later. If you need to support XP (or earlier) then temporary file followed by a move (rename) is the best solution.

这个类似问题的答案提供了更多信息:File.Move 的原子性

The answer to this similar question provides more info: Atomicity of File.Move

这篇关于.NET下的原子文件复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆