FileSystemWatcher 类 - 排除目录

发布于 2024-11-08 17:20:46 字数 1025 浏览 1 评论 0原文

我目前正在尝试使用 FileSystemWatcher 类排除目录,尽管我已经使用了这个:

FileWatcher.Filter = "C:\\$Recycle.Bin";

并且

FileWatcher.Filter = "$Recycle.Bin";

它编译正常,但是当我尝试这样做时没有显示结果。

如果我取出过滤器,所有文件都会加载正常,代码如下:

 static void Main(string[] args)
        {
            string DirPath = "C:\\";

            FileSystemWatcher FileWatcher = new FileSystemWatcher(DirPath);
            FileWatcher.IncludeSubdirectories = true;
            FileWatcher.Filter = "*.exe";
          // FileWatcher.Filter = "C:\\$Recycle.Bin";
          //  FileWatcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);
            FileWatcher.Created += new FileSystemEventHandler(FileWatcher_Created);
          //  FileWatcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);
          //  FileWatcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);
            FileWatcher.EnableRaisingEvents = true;

            Console.ReadKey();
        }

I am currently trying to exclude directories with the FileSystemWatcher class, although I have used this:

FileWatcher.Filter = "C:\\$Recycle.Bin";

and

FileWatcher.Filter = "$Recycle.Bin";

It compiles ok, but no results are shown when I try this.

If I take the filter out, all files load fine, code is below:

 static void Main(string[] args)
        {
            string DirPath = "C:\\";

            FileSystemWatcher FileWatcher = new FileSystemWatcher(DirPath);
            FileWatcher.IncludeSubdirectories = true;
            FileWatcher.Filter = "*.exe";
          // FileWatcher.Filter = "C:\\$Recycle.Bin";
          //  FileWatcher.Changed += new FileSystemEventHandler(FileWatcher_Changed);
            FileWatcher.Created += new FileSystemEventHandler(FileWatcher_Created);
          //  FileWatcher.Deleted += new FileSystemEventHandler(FileWatcher_Deleted);
          //  FileWatcher.Renamed += new RenamedEventHandler(FileWatcher_Renamed);
            FileWatcher.EnableRaisingEvents = true;

            Console.ReadKey();
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笙痞 2024-11-15 17:20:46

您可能还没有阅读 http://msdn.microsoft .com/en-us/library/system.io.filesystemwatcher.filter.aspx。您不能使用 Filter 属性排除任何内容。它仅包含匹配过滤器的对象。

如果您想排除某些内容,请在 FSW 触发的事件中执行此操作。

You probably haven't read http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.filter.aspx. You cannot exclude anything with Filter property. It only includes objects matching filter.

If you want exclude something, do it in events fired by FSW.

南冥有猫 2024-11-15 17:20:46

确定该文件是否是事件处理程序中的目录,然后不执行任何操作:

private void WatcherOnCreated(object sender, FileSystemEventArgs fileSystemEventArgs)
{
    if (File.GetAttributes(fileSystemEventArgs.FullPath).HasFlag(FileAttributes.Directory))
        return; //ignore directories, only process files

    //TODO: Your code handling files...
}

Determine if the file is a directory in your event handler, and do nothing then:

private void WatcherOnCreated(object sender, FileSystemEventArgs fileSystemEventArgs)
{
    if (File.GetAttributes(fileSystemEventArgs.FullPath).HasFlag(FileAttributes.Directory))
        return; //ignore directories, only process files

    //TODO: Your code handling files...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文