【问题标题】:FileSystemWatcher calls handler three times [duplicate]FileSystemWatcher 调用处理程序三次 [重复]
【发布时间】:2013-10-14 20:27:30
【问题描述】:

我编写了一个测试应用程序来试用来自msdn 的 FileSystemWatcher 示例代码。

它基本上可以工作,但处理程序被调用了 3 次。有人知道为什么吗?

namespace FileWatcherTest
{
    public partial class Form1 : Form
    {
        private FileSystemWatcher watcher;
        public Form1()
        {
            InitializeComponent();
            string testPath = 
                Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) 
                + @"\Test";
            InitialiseFileWatcher(testPath, "example.txt");
        }

        private void InitialiseFileWatcher(string path, string fileName)
        {
            watcher = new FileSystemWatcher();
            watcher.Path = path;
            watcher.NotifyFilter = NotifyFilters.LastWrite;
            watcher.Filter = fileName;
            watcher.Changed += new FileSystemEventHandler(OnFarmListChanged);
            // Begin watching.
            watcher.EnableRaisingEvents = true;
        }

        private static void OnFarmListChanged(object source, FileSystemEventArgs e)
        {
            string text = "File: " + e.FullPath + " " + e.ChangeType;
            MessageBox.Show(text);
        }
    }
}

【问题讨论】:

    标签: c# filesystemwatcher


    【解决方案1】:

    作为您包含的链接中文档的一部分:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-16
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多