How to set filter for FileSystemWatcher for multiple file types?

后端 未结 6 1130
醉酒成梦
醉酒成梦 2020-12-08 13:16

Everywhere I find these two lines of code used to set filter for file system watcher in samples provided..

FileSystemWatcher watcher = new FileSystemWatcher(         


        
6条回答
  •  忘掉有多难
    2020-12-08 13:41

    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,
        };
    
    

提交回复
热议问题