以编程方式移动一批文件的安全方法?

发布于 2024-10-10 20:45:38 字数 291 浏览 2 评论 0原文

创建将一批文件从一个目录移动(剪切)到另一个目录的方法时需要注意什么?

假设方法签名是 Move(filter, sourceFolder, destinationFolder, overwrite)。我需要注意什么才能避免数据丢失的风险,特别是在覆盖原始文件并考虑到删除源文件的情况下?

我担心的几种可能的情况:移动正在进行时发生错误,移动文件但文件以某种方式损坏,删除同名文件以允许新文件移动,但移动新文件时发生错误,等等。

我使用 .net 的 System.IO 命名空间进行移动操作。

What does one need to take care of when creating a method to move (cut) a batch of file from one directory to another?

Let's say the method signature is Move(filter, sourceFolder, destinationFolder, overwrite). What do I need to take care of to avoid the risk of data loss especially when overwriting the original file and deleting of the source file is taken into account?

Several possible scenario I am worried of: error occurs when a move is in progress, moved a file but the file are somehow corrupted, deleted a namesake file in order to allow the new file to move but then error happens when moving the new file, etc.

I'm using .net's System.IO namespace for the move operations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错爱 2024-10-17 20:45:38

没有交易,最安全的方式就是复制、验证然后删除。如果您想移动每个文件(Windows 就是这样做的,移动操作可能会失败,导致您移动了一半的文件),或者只允许移动整个批次,或者根本不移动,这取决于您。

您必须决定如何响应移动期间已修改的文件、之后无法删除的源文件或执行回滚时已打开的目标文件。

Without transactions, the safest way is to copy, verify and then delete. It is up to you if you want to move per file (this is how windows does it, a move operation can fail leaving you with half of the files moved) or to allow only the entire batch to be moved, or none at all.

You would have to make decisions on how to respond to files that have been modified during the move, source files that cannot be deleted afterwards, or destination files that have already been opened when you're performing a rollback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文