如何为多种文件类型的FileSystemWatcher设置过滤器? [英] How to set filter for FileSystemWatcher for multiple file types?
本文介绍了如何为多种文件类型的FileSystemWatcher设置过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在提供的示例中,到处都可以找到这两行代码来设置文件系统监视程序的过滤器。
Everywhere I find these two lines of code used to set filter for file system watcher in samples provided..
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Filter = "*.txt";
//or
watcher.Filter = "*.*";
但是我希望我的观察者监视更多的文件类型,但不是全部。我该如何实现:
But I want my watcher to monitor more file types, but not all. How can I achieve this:
//watcher.Filter = "*.txt" | "*.doc" | "*.docx" | "*.xls" | "*.xlsx";
我尝试了以下操作:
watcher.Filter = "*.txt|*.doc|*.docx|*.xls|*.xlsx";
// and
watcher.Filter = "*.txt;*.doc;*.docx;*.xls;*.xlsx*";
两者均无效。这只是基础知识,但我想念它。谢谢。。
Both did not work. This is just basics but I miss it. Thanks..
推荐答案
由于.Net Core 3.x和.Net 5 Preview的存在,您只需将多个过滤器添加到 过滤
集合。
Since .Net Core 3.x and .Net 5 Preview you can simply add multiple filters to the Filters
collection.
var watcher = new FileSystemWatcher();
watcher.Path = "/your/path";
watcher.Filters.Add("*.yml");
watcher.Filters.Add("*.yaml");
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.EnableRaisingEvents = true;
或者,如果您喜欢对象初始值设定项,
Alternatively if you like object initializers,
var watcher = new FileSystemWatcher
{
Path = "/your/path",
Filters = {"*.yml", "*.yaml"},
NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName,
EnableRaisingEvents = true,
};
这篇关于如何为多种文件类型的FileSystemWatcher设置过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文