尝试删除文件时根据文件大小更改超时的最佳方法是什么
我想确保我正在移动的文件在其目的地不存在。这就是我正在做的事情
// delete if exists already
if (File.Exists(target))
{
File.Delete(target);
}
// move to target
File.Move(source, target);
,但有时,当程序点击 File.Move 时,文件并未完全删除。为了解决这个问题,我计划使用 FileSystemWatcher 在文件被成功删除后恢复流程。但我不想永远等待,所以我想在 FileSystemWatcher 上设置一个超时,以便在一段时间后即使文件没有被删除,它也会恢复流程。
我想让超时成为文件大小的函数。假设文件大小为 1MB,超时时间为 1 秒,但如果文件大小为 10MB,则超时时间为 10 秒。现在有没有人选择这个超时的最佳方法,或者我应该使用固定的超时,无论文件大小如何。
I want to make sure the file I'm moving doesn't exist at it's destination. This is what I'm doing
// delete if exists already
if (File.Exists(target))
{
File.Delete(target);
}
// move to target
File.Move(source, target);
But sometime, the file is not completely deleted when the program hit File.Move. To fix this I'm planning on using a FileSystemWatcher to resume the flow after the file have been successfully deleted. But I don't want to wait for ever so i want to put a timeout on the FileSystemWatcher so that after a while it resume the flow even if the file is not deleted.
I would like to make that timeout a function of the size of the file. So let's say if the file is 1MB, the timeout would be 1 second but if it was 10MB it would be 10 second. Does anybody now the best way to choose this timeout or should I just use a fixed timeout what ever the size of the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
反过来做:(
如果有一个 File.Move(string, string, bool),我会说使用它,但没有。)
Do it the other way around:
(If there were a
File.Move(string, string, bool)
, I'd say use that, but there isn't.)