C#中的WatchFolder事件在复制文件上触发多次 [英] WatchFolder event in C# triggers multiple times on a copy file

查看:92
本文介绍了C#中的WatchFolder事件在复制文件上触发多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码来练习

I am using the below code to practice FileSystemWatcher.Changed Event from here. It perfectly works fine with .txt extension, but the output is not as I expect to see when I use .pdf. It triggers one "Created" events and multiple "Changed" events.

using System;
using System.IO;
using System.Security.Permissions;

public class Watcher
{

    public static void Main()
    {
        Run();

    }

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    public static void Run()
    {
        string[] args = System.Environment.GetCommandLineArgs();

        // If a directory is not specified, exit program.
        if (args.Length != 2)
        {
            // Display the proper way to call the program.
            Console.WriteLine("Usage: Watcher.exe (directory)");
            return;
        }

        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = args[1];
        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        // Only watch text files.
        watcher.Filter = "*.pdf";

        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        // Begin watching.
        watcher.EnableRaisingEvents = true;

        // Wait for the user to quit the program.
        Console.WriteLine("Press \'q\' to quit the sample.");
        while (Console.Read() != 'q') ;
    }

    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
        Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
    }

    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        // Specify what is done when a file is renamed.
        Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }
}

这是我将PDF文件复制到监视文件夹时的输出:

Here is the output when I copy a PDF file to watching folder:

File: c:\test\innovation.pdf Created
File: c:\test\innovation.pdf Changed
File: c:\test\innovation.pdf Changed
File: c:\test\innovation.pdf Changed
File: c:\test\innovation.pdf Changed

知道为什么会这样吗?

推荐答案

感谢艾哈迈德·伊利亚斯(Ahmed Ilyas).我要做的就是更改:

Thanks to Ahmed Ilyas. All I needed to do was to change:

watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
       | NotifyFilters.FileName | NotifyFilters.DirectoryName;

watcher.NotifyFilter = NotifyFilters.FileName;

这篇关于C#中的WatchFolder事件在复制文件上触发多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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