FileNotFoundException异常在FileSystemWatcher的异常、FileNotFoundException、FileSystemWatcher

2023-09-06 23:15:29 作者:安生

我在一个目录使用 FileSystemWatcher的并添加了事件处理程序,将其 EnableRaisingEvents = TRUE; IncludeSubdirectories = FALSE; 并添加 NotifyFilters

I am using a FileSystemWatcher on a directory and added its event handlers, set its EnableRaisingEvents=true; and IncludeSubdirectories=false; and added NotifyFilters.

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

While running the application if I create new folders in the specified directory sometime I get

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

FileNotFoundException : "An error occurred while reading a directory". System.IO.FileSystemWatcher.StartRaisingEvents() System.IO.FileSystemWatcher.set_EnableRaisingEvents(Boolean value)

什么是问题的根源在哪里?

What can be root cause of the problem?

什么是 StartRaisingEvents()

推荐答案

这是典型的因为 FileSystemWatcher的可能是不可靠的。该文件夹可能不是完全当你的事件存在。您可能需要重试足够的停顿,做各种 Directory.Exists()在实际执行IO操作检查。

This is typically because the FileSystemWatcher can be unreliable. The folder may not "fully" exist when you get the events. You may need to retry with sufficient pauses and do various Directory.Exists() checks before actually performing IO operations.