简体   繁体   English

如何确定哪个文件过滤器与 FileSystemWatcher 事件匹配?

[英]How can I determine which file filter was matched for a FileSystemWatcher event?

I am using a FileSystemWatcher as follows:我正在使用FileSystemWatcher ,如下所示:

using watcher = new FileSystemWatcher(@"C:\path\to\folder");

watcher.NotifyFilter = NotifyFilters.Attributes
                     | NotifyFilters.CreationTime
                     | NotifyFilters.DirectoryName
                     | NotifyFilters.FileName
                     | NotifyFilters.LastAccess
                     | NotifyFilters.LastWrite
                     | NotifyFilters.Security
                     | NotifyFilters.Size;

watcher.Created += OnCreated;
watcher.Error += OnError;

foreach(string filter in filters) 
{
    watcher.Filters.Add(filter);
}

watcher.IncludeSubdirectories = false;
watcher.EnableRaisingEvents = true;

The OnCreated event handler is defined as follows: OnCreated事件处理程序定义如下:

private static void OnCreated(object sender, FileSystemEventArgs e)
{
    string value = $"Created: {e.FullPath}";
    Console.WriteLine(value);
}

Now, I want to know if is there a way I can find from exactly which filter specified in the Filters list has the event been raised?现在,我想知道是否有一种方法可以准确地从“ Filters ”列表中指定的哪个过滤器中找到事件已引发?

For example,例如,

  1. I am watching C:\FileWatcherDemo with the OnCreated event handler.我正在使用OnCreated事件处理程序观看C:\FileWatcherDemo
  2. "f1_\*.txt" and "f2_\*.txt" are added to Filters . "f1_\*.txt""f2_\*.txt"被添加到Filters中。
  3. I add "f1_demo.txt" to the folder.我将"f1_demo.txt"添加到文件夹中。
  4. I have the sender object and the FileSystemEventArgs passed as argument to my OnCreated handler.我有sender object 和FileSystemEventArgs作为参数传递给我的OnCreated处理程序。

How can I know that the OnCreated event was actually matched by the "f1_*.txt" filter?我怎么知道OnCreated事件实际上是由"f1_*.txt"过滤器匹配的?

You may wish to consider using Microsoft's Reactive Framework (aka Rx) - NuGet System.Reactive and add using System.Reactive.Linq;您可能希望考虑使用 Microsoft 的 Reactive Framework (aka Rx) - NuGet System.Reactiveusing System.Reactive.Linq; - then you can do this: - 然后你可以这样做:

IObservable<(NotifyFilters NotifyFilter, FileSystemEventArgs FileSystemEventArgs)> query =
    from nf in new []
    {
        NotifyFilters.Attributes,
        NotifyFilters.CreationTime,
        NotifyFilters.DirectoryName,
        NotifyFilters.FileName,
        NotifyFilters.LastAccess,
        NotifyFilters.LastWrite,
        NotifyFilters.Security,
        NotifyFilters.Size,
    }.ToObservable()
    from u in Observable.Using(
        () => new FileSystemWatcher(@"C:\path\to\folder"),
        fsw => Observable.FromEventPattern<FileSystemEventHandler, FileSystemEventArgs>(h => fsw.Created += h, h => fsw.Created -= h))
    select (nf, u.EventArgs);
    

IDisposable subscription =
    query.Subscribe(x =>
    {
        Console.WriteLine($"Created: {x.FileSystemEventArgs.FullPath}");
        Console.WriteLine($"Created: {x.NotifyFilter}");
    });

It effectively creates separate FileSystemWatcher instances for each filter yet produces a single merged stream of events preserving the NotifyFilter used.它有效地为每个过滤器创建单独的FileSystemWatcher实例,但生成单个合并的 stream 事件,保留使用的NotifyFilter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM