FileNotFoundException in FileSystemWatcher

微笑、不失礼 提交于 2020-01-05 08:38:43

问题


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 : "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?

What is StartRaisingEvents()?


回答1:


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.




回答2:


I've got quite the same problem and finally I found out that the problem was with the path.

The Directory.Exist() give answer that the directory exist... even if the path got a empty char in the end of the string but the FileSystemWatcher couldn't manage it. So obviously the Directory.Exist() trim the path but the Watcher don't. In my case removing of the empty chars solve the problem.

Hopefully it could help somebody.



来源:https://stackoverflow.com/questions/3976862/filenotfoundexception-in-filesystemwatcher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!