-->

FileNotFoundException异常在FileSystemWatcher的(FileNot

2019-09-27 18:14发布

我使用的是FileSystemWatcher一个目录,并添加了事件处理程序,设置其EnableRaisingEvents=true;IncludeSubdirectories=false; 并补充NotifyFilters

当运行应用程序,如果我在指定的目录中创建新的文件夹有时我得到

FileNotFoundException异常:“当读取一个目录时出错”。 System.IO.FileSystemWatcher.StartRaisingEvents()System.IO.FileSystemWatcher.set_EnableRaisingEvents(布尔值)

可能是什么问题的根本原因?

什么是StartRaisingEvents()

Answer 1:

这通常是因为FileSystemWatcher是不可靠的。 该文件夹可能不是“完全”当你的事件存在。 您可能需要有足够的暂停重试和做各种各样的Directory.Exists()实际执行IO操作前检查。



Answer 2:

我有完全一样的问题,最后我发现这个问题是与路径。

Directory.Exist()给该目录中存在的答案...即使路径有一个空字符的字符串的结束,但FileSystemWatcher不能管理它。 如此明显的Directory.Exist()修剪路径,但看守没有。 在我的情况下取出空字符的解决这个问题。

希望它可以帮助别人。



文章来源: FileNotFoundException in FileSystemWatcher