等待系统删除文件 [英] Waiting for system to delete file

查看:115
本文介绍了等待系统删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除文件后,我在刷新文件列表时遇到了问题。当我给命令删除文件,抛出异常,因为刷新方法试图访问应该被删除的文件。



经过一些思考和调试,我来得出结论,问题在于系统需要一些时间来删除文件。我解决这个问题:

$ p $ //删除文件
System.Threading.Thread.Sleep(2000);
//刷新列表

它工作正常。



我的问题是

有没有更优雅的方法来等待系统删除文件,然后继续执行代码...?

解决方案

我能想到的最优雅的方式是使用 FileSystemWatcher 并订阅其已删除的事件。

I had a problem with refreshing file list after deleting a file. When I gave command to delete file, the exception was thrown because the refresh method tried to access a file that was supposed to be deleted.

After some thought and debuging I came to conclusion that problem was in a fact that system needs some time to delete a file. And I solve it like this:

//Deleting file
System.Threading.Thread.Sleep(2000);
//Refreshing list

and it worked fine.

My question is

Is there a more elegant way to wait for system do delete file and then continue with code...?

解决方案

The most elegant way I can think of is using a FileSystemWatcher and subscribe to its Deleted event.

这篇关于等待系统删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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