Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FileSystemWatcher调用处理程序三次_C#_Filesystemwatcher - Fatal编程技术网

C# FileSystemWatcher调用处理程序三次

C# FileSystemWatcher调用处理程序三次,c#,filesystemwatcher,C#,Filesystemwatcher,我已经编写了一个测试应用程序来试用来自的FileSystemWatcher示例代码 它基本上可以工作,但处理程序会被调用三次。有人知道为什么吗 namespace FileWatcherTest { public partial class Form1 : Form { private FileSystemWatcher watcher; public Form1() { InitializeComponen

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

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

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);
        }
    }
}

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

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