閱讀英文版本

分享方式:


NotifyFilters 列舉

定義

指定在檔案或資料夾中要監看的變更。

此列舉支援其成員值的位元組合。

C#
[System.Flags]
public enum NotifyFilters
繼承
NotifyFilters
屬性

欄位

名稱 Description
Attributes 4

檔案或資料夾的屬性。

CreationTime 64

檔案或資料夾建立的時間。

DirectoryName 2

目錄的名稱。

FileName 1

檔案的名稱。

LastAccess 32

檔案或資料夾上次開啟的日期。

LastWrite 16

檔案或資料夾上次有資料寫入的日期。

Security 256

檔案或資料夾的安全性設定。

Size 8

檔案或資料夾的大小。

範例

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

C#
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 一種以上的變更。 例如,您可以 watch 檔案或資料夾的大小變更,以及安全性設定中的變更。 每當檔案或資料夾的大小或安全性設定有所變更時,就會引發事件。

適用於

產品 版本
.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

另請參閱