使用英语阅读

通过


FileSystemWatcher.EnableRaisingEvents 属性

定义

获取或设置一个值,该值指示是否启用此组件。

public bool EnableRaisingEvents { get; set; }
[System.IO.IODescription("FSW_Enabled")]
public bool EnableRaisingEvents { get; set; }

属性值

如果启用此组件,则为 true;否则为 false。 默认值为 false。 如果在 Visual Studio 2005 中的设计器上使用 组件,则默认值为 true

属性

例外

当前操作系统不是 Microsoft Windows NT 或更高版本。

未能找到 Path 中指定的目录。

Path 尚未设置或无效。

示例

以下示例创建 ,FileSystemWatcher以watch在运行时指定的目录。 组件设置为watch,以便更改LastWriteLastAccess时间,以及创建、删除或重命名目录中的文本文件。 如果更改、创建或删除了某个文件,该文件的路径将输出到控制台。 重命名文件时,新旧路径将打印到控制台。

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

注解

除非将 设置为 EnableRaisingEventstrue,否则组件不会引发事件。

备注

在设置 属性并且 EnableRaisingEventstrue之前,Path组件不会watch指定的目录。

方法 WaitForChanged 允许调用事件处理程序以响应文件更改,即使此属性设置为 false也是如此。

适用于

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