FileSystemWatcher

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        // 创建一个新的FileSystemWatcher对象
        FileSystemWatcher watcher = new FileSystemWatcher();

        // 设置要监视的目录路径
        watcher.Path = @"C:\MyFolder";

        // 设置要监视的文件和文件夹更改类型
        watcher.NotifyFilter = NotifyFilters.LastWrite
                             | NotifyFilters.FileName
                             | NotifyFilters.DirectoryName;

        // 只监视文本文件类型
        watcher.Filter = "*.txt";

        // 添加事件处理程序
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnCreated);
        watcher.Deleted += new FileSystemEventHandler(OnDeleted);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);

        // 开始监视
        watcher.EnableRaisingEvents = true;

        // 等待用户输入
        Console.WriteLine("Press 'q' to quit the sample.");
        while (Console.Read() != 'q') ;
    }

    // 文件或文件夹更改事件处理程序
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        Console.WriteLine($"File {e.FullPath} has been changed.");
    }

    // 文件或文件夹创建事件处理程序
    private static void OnCreated(object source, FileSystemEventArgs e)
    {
        Console.WriteLine($"File {e.FullPath} has been created.");
    }

    // 文件或文件夹删除事件处理程序
    private static void OnDeleted(object source, FileSystemEventArgs e)
    {
        Console.WriteLine($"File {e.FullPath} has been deleted.");
    }

    // 文件或文件夹重命名事件处理程序
    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        Console.WriteLine($"File {e.OldFullPath} has been renamed to {e.FullPath}");
    }
}