FileSystemWatcher.Changed 事件

定义

当更改指定 Path 中的文件和目录时发生。

public event System.IO.FileSystemEventHandler? Changed;
public event System.IO.FileSystemEventHandler Changed;
[System.IO.IODescription("FSW_Changed")]
public event System.IO.FileSystemEventHandler Changed;

事件类型

属性

示例

以下示例使用 Changed 事件在所监视文件发生更改时显示控制台的文件路径。

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

注解

Changed 对所监视目录中文件或目录的大小、系统属性、上次写入时间、上次访问时间或安全权限进行更改时,将引发 事件。

备注

常见的文件系统操作可能会引发多个事件。 例如,当文件从一个目录移动到另一个目录时,可能会引发多个 OnChanged 和一些 OnCreatedOnDeleted 事件。 移动文件是一项复杂的操作,由多个简单操作组成,因此引发多个事件。 同样,某些应用程序 (例如,防病毒软件) 可能会导致 检测到 FileSystemWatcher的其他文件系统事件。

用于 NotifyFilter 限制处理此事件时引发的通知数。

备注

重命名 Changed 文件时会意外引发 事件,但在重命名目录时不会引发事件。 若要watch重命名,请使用 Renamed 事件。

备注

当 属性不是 nullSynchronizingObjectChanged相对于其他FileSystemWatcher事件引发事件的顺序可能会更改。

适用于

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

另请参阅