FileSystemWatcher.Filter 屬性

定義

取得或設定篩選字串,用以判斷在目錄中監視什麼檔案。

public string Filter { get; set; }
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[System.IO.IODescription("FSW_Filter")]
public string Filter { get; set; }
[System.IO.IODescription("FSW_Filter")]
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string Filter { get; set; }
[System.IO.IODescription("FSW_Filter")]
[System.ComponentModel.SettingsBindable(true)]
[System.ComponentModel.TypeConverter("System.Diagnostics.Design.StringValueConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string Filter { get; set; }
[System.ComponentModel.SettingsBindable(true)]
public string Filter { get; set; }

屬性值

篩選條件字串。 預設值為 "*.*" (監看所有檔案)。

屬性

範例

下列範例會FileSystemWatcher建立 ,以 watch 運行時間指定的目錄。 元件會設定為 watch,以便LastWrite變更和LastAccess時間、建立、刪除或重新命名目錄中的文本檔。 如果檔案已變更、建立或刪除,則檔案的路徑會列印至主控台。 重新命名檔案時,舊路徑和新路徑會列印到主控台。

using System;
using System.IO;

namespace MyNamespace
{
    class MyClassCS
    {
        static void Main()
        {
            using var watcher = new FileSystemWatcher(@"C:\path\to\folder");

            watcher.NotifyFilter = NotifyFilters.Attributes
                                 | NotifyFilters.CreationTime
                                 | NotifyFilters.DirectoryName
                                 | NotifyFilters.FileName
                                 | NotifyFilters.LastAccess
                                 | NotifyFilters.LastWrite
                                 | NotifyFilters.Security
                                 | NotifyFilters.Size;

            watcher.Changed += OnChanged;
            watcher.Created += OnCreated;
            watcher.Deleted += OnDeleted;
            watcher.Renamed += OnRenamed;
            watcher.Error += OnError;

            watcher.Filter = "*.txt";
            watcher.IncludeSubdirectories = true;
            watcher.EnableRaisingEvents = true;

            Console.WriteLine("Press enter to exit.");
            Console.ReadLine();
        }

        private static void OnChanged(object sender, FileSystemEventArgs e)
        {
            if (e.ChangeType != WatcherChangeTypes.Changed)
            {
                return;
            }
            Console.WriteLine($"Changed: {e.FullPath}");
        }

        private static void OnCreated(object sender, FileSystemEventArgs e)
        {
            string value = $"Created: {e.FullPath}";
            Console.WriteLine(value);
        }

        private static void OnDeleted(object sender, FileSystemEventArgs e) =>
            Console.WriteLine($"Deleted: {e.FullPath}");

        private static void OnRenamed(object sender, RenamedEventArgs e)
        {
            Console.WriteLine($"Renamed:");
            Console.WriteLine($"    Old: {e.OldFullPath}");
            Console.WriteLine($"    New: {e.FullPath}");
        }

        private static void OnError(object sender, ErrorEventArgs e) =>
            PrintException(e.GetException());

        private static void PrintException(Exception? ex)
        {
            if (ex != null)
            {
                Console.WriteLine($"Message: {ex.Message}");
                Console.WriteLine("Stacktrace:");
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine();
                PrintException(ex.InnerException);
            }
        }
    }
}

備註

若要 watch 所有檔案中的變更,請將 Filter 屬性設定為空字串 (“”) 。 若要 watch 特定檔案,請將 Filter 屬性設定為檔名。 例如,若要 watch 檔案 MyDoc.txt 變更,請將 Filter 屬性設定為 “MyDoc.txt”。 您也可以 watch 特定檔案類型中的變更。 例如,若要 watch 變更任何文本檔,請將 Filter 屬性設定為 “*.txt”。 不支援使用多個篩選,例如 「*.txt|*.doc」。。

Filter屬性可以在 對象開始接收事件之後FileSystemWatcher變更。

如需篩選掉垃圾通知的詳細資訊,請參閱 NotifyFilterIncludeSubdirectoriesInternalBufferSize 屬性。

Filter 接受符合檔案的通配符,如下列範例所示。

篩選字串 監看下列檔案
*.* 所有檔案 (預設) 。 空字串 (“”) 也會監看所有檔案。
*.txt 所有擴展名為 「txt」 的檔案。
*recipe.doc 所有結尾為 「recipe」 且擴展名為 「doc」 的檔案。
win*.xml 開頭為 「win」 且擴展名為 「xml」 的所有檔案。
Sales*200?.xls 符合下列專案:

- 7 月銷售 2001.xls
- 銷售 8 月 2002.xls
- 銷售 3 月 2004.xls

但不符合:

- 銷售 11 月 1999.xls
MyReport.Doc 僅監看 MyReport.doc

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

另請參閱