【问题标题】:WatchFolder event in C# triggers multiple times on a copy file [duplicate]C#中的WatchFolder事件在复制文件上多次触发[重复]
【发布时间】:2015-11-30 02:29:15
【问题描述】:

我正在使用下面的代码来练习来自hereFileSystemWatcher.Changed 事件。它与 .txt 扩展名完美配合,但输出与我使用 .pdf 时的预期不同。它会触发一个“Created”事件和多个“Changed”事件。

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 文件复制到观看文件夹时的输出:

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

知道为什么会这样吗?

【问题讨论】:

标签: c# filesystemwatcher


【解决方案1】:

感谢艾哈迈德·伊利亚斯。我需要做的就是改变:

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

watcher.NotifyFilter = NotifyFilters.FileName;

【讨论】:

  • 虽然这是正确的,但它并不能解释 txt 和 pdf 之间的区别。
猜你喜欢
  • 2020-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-01
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 2015-03-24
相关资源
最近更新 更多