以编程方式移动一批文件的安全方法?
创建将一批文件从一个目录移动(剪切)到另一个目录的方法时需要注意什么?
假设方法签名是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有交易,最安全的方式就是复制、验证然后删除。如果您想移动每个文件(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.