FileSystemWatcher的NotifyFilter不起作用 [英] NotifyFilter of FileSystemWatcher not working

查看:248
本文介绍了FileSystemWatcher的NotifyFilter不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows服务(并通过创建类似的WinForms应用程序验证了代码),其中NotifyFilter不起作用.删除该行代码后,该服务将正常运行,并且可以在WinForms应用程序中看到事件处理程序触发.

I have a windows service (and verified the code by creating a similar WinForms application) where the NotifyFilter doesn't work. As soon as I remove that line of code, the service works fine and I can see the event-handler fire in the WinForms application.

我正在做的是将一个文本文件拖放到FileSystemWatcher的输入目录中,以启动watcher_FileChanged委托.当我在其中放置_watcher.NotifyFilter = NotifyFilters.CreationTime;时,它不起作用.当我将其拔出时,效果很好.

All I'm doing is dropping a text file into the input directory for the FileSystemWatcher to kick off the watcher_FileChanged delegate. When I have the _watcher.NotifyFilter = NotifyFilters.CreationTime; in there, it doesn't work. When I pull it out, it works fine.

有人可以告诉我此过滤器是否做错了吗?

Can anyone tell me if I'm doing something wrong with this filter?

这是OnStart事件的FSW代码.

Here is the FSW code for the OnStart event.

protected override void OnStart(string[] args)
{
    _watcher = new FileSystemWatcher(@"C:\Projects\Data\Test1");

    _watcher.Created += new FileSystemEventHandler(watcher_FileChanged);
    _watcher.NotifyFilter = NotifyFilters.CreationTime;
    _watcher.IncludeSubdirectories = false;
    _watcher.EnableRaisingEvents = true;
    _watcher.Error += new ErrorEventHandler(OnError);    
}

private void watcher_FileChanged(object sender, FileSystemEventArgs e)
{
    // Folder with new files - one or more files
    string folder = @"C:\Projects\Data\Test1";
    System.Console.WriteLine(@"C:\Projects\Data\Test1");
    //Console.ReadKey(true); 

    // Folder to delete old files - one or more files
    string output = @"C:\Temp\Test1\";
    System.Console.WriteLine(@"C:\Temp\Test1\");
    //Console.ReadKey(true);

    // Create name to call new zip file by date
    string outputFilename = Path.Combine(output, string.Format("Archive{0}.zip", DateTime.Now.ToString("MMddyyyy")));
    System.Console.WriteLine(outputFilename);
    //Console.ReadKey(true);

    // Save new files into a zip file
    using (ZipFile zip = new ZipFile())
    {
        // Add all files in directory
        foreach (var file in Directory.GetFiles(folder))
        {
            zip.AddFile(file);
        }

        // Save to output filename
        zip.Save(outputFilename);
    }

    DirectoryInfo source = new DirectoryInfo(output);
    // Get info of each file into the output directory to see whether or not to delete
    foreach (FileInfo fi in source.GetFiles())
    {
        if (fi.CreationTime < DateTime.Now.AddDays(-1))
            fi.Delete();
    }
}

推荐答案

我也遇到了这种问题.如果您单步执行代码(并且如果您查看 MSDN,文档,您会发现NotifyFilter开始时的默认值为:

I've been having trouble with this behavior too. If you step through the code (and if you look at MSDN documenation, you'll find that NotifyFilter starts off with a default value of:

NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite

因此,当您说.NotifyFilter = NotifyFilters.CreationTime时,您将抹去其他值,这解释了行为上的差异.我不确定为什么NotifyFilters.CreationTime没有捕获新文件...看起来应该,应该不!

So when you say .NotifyFilter = NotifyFilters.CreationTime, you're wiping out those other values, which explains the difference in behavior. I'm not sure why NotifyFilters.CreationTime is not catching the new file... seems like it should, shouldn't it!

如果对您有用,您可以仅使用NotifyFilter的默认值.如果要添加NotifyFilters.CreationTime,我建议您执行以下操作以添加新值,而不是替换现有值:

You can probably just use the default value for NotifyFilter if it's working for you. If you want to add NotifyFilters.CreationTime, I'd recommend doing something like this to add the new value and not replace the existing ones:

_watcher.NotifyFilter = _watcher.NotifyFilter | NotifyFilters.CreationTime;

这篇关于FileSystemWatcher的NotifyFilter不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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