FileSystemWatcher 类 - 排除目录
我目前正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能还没有阅读 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.
确定该文件是否是事件处理程序中的目录,然后不执行任何操作:
Determine if the file is a directory in your event handler, and do nothing then: