这是我的代码:
_instrumentsFolderWatcher.Changed += _instrumentsFolderWatcher_Changed;
_instrumentsFolderWatcher.Created += _instrumentsFolderWatcher_Created;
_instrumentsFolderWatcher.Deleted += _instrumentsFolderWatcher_Deleted;
其中_instrumentsFolderWatcher
是FileSystemWatcher
的实例。问题是当我将文件粘贴到正在监视的文件夹中时,创建的事件和更改的事件都会被触发。为什么更改的事件会被解雇?此外,changed 事件是通过 Changed 的 ChangeType
触发的。我该如何解决这个问题?
编辑:
为什么当我更改文件名时未触发 Changed 事件?
最佳答案
Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.
尝试使用 Renamed
事件来确定文件何时被重命名。
关于c# - 为什么 FileSystemWatcher 在创建文件时引发 Changed 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19417819/