NotifyFilters 枚举

定义

指定要在文件或文件夹中监视的更改。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum NotifyFilters
继承
NotifyFilters
属性

字段

名称 说明
Attributes 4

文件或文件夹的属性。

CreationTime 64

文件或文件夹的创建时间。

DirectoryName 2

目录名。

FileName 1

文件的名称。

LastAccess 32

文件或文件夹上一次打开的日期。

LastWrite 16

上一次向文件或文件夹写入内容的日期。

Security 256

文件或文件夹的安全设置。

Size 8

文件或文件夹的大小。

示例

以下示例创建 以FileSystemWatcherwatch在运行时指定的目录。 对于 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

另请参阅