FileSystemWatcher收到太多事件 [英] FileSystemWatcher getting too many event

查看:100
本文介绍了FileSystemWatcher收到太多事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个文件监视程序,如果文件内容被更改,它将引发一个事件. 一旦我正在观看的文件被修改,我将收到2个事件的问题. (我只想得到一次)

I'm trying to implement a file watcher that will raise an event if the file content was changed. The problem that once the file I am watching was modified, I am getting 2 events. (I want to get it only once)

_automationStatusWatcher = new FileSystemWatcher(fileInfo.Directory.FullName,
                                                 fileInfo.Name);
_automationStatusWatcher.NotifyFilter = NotifyFilters.LastWrite;
_automationStatusWatcher.Changed += OnAutomationStatusChanged;
_automationStatusWatcher.EnableRaisingEvents = true;

我正在观看的文件无意于重新创建/移动/删除/执行任何操作.它的目的是成为一个xml数据库文件,一旦它发生更改,我就需要对其进行监视.修改文件后,我只希望获得1个事件,而忽略其他事件.

The file that i'm watching is not intended to be recreated/moved/deleted/whatever. its purpose is to be an xml database file that i need to monitor it once it changes. i want to get only 1 event when the file is modified and to ignore the other events.

我该怎么做?

推荐答案

手册:

注意

常见文件系统操作可能引发多个事件.例如,当文件从一个目录移动到另一个目录时,可能会引发几个OnChanged以及一些OnCreated和OnDeleted事件.移动文件是一个复杂的操作,由多个简单的操作组成,因此会引发多个事件.同样,某些应用程序(例如,防病毒软件)可能会导致FileSystemWatcher检测到其他文件系统事件.

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.

这篇关于FileSystemWatcher收到太多事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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