关于c#:使用FileWatcher检测文件夹中是否有新文件创建

Detect if there are new files created in a folder using FileWatcher

我想知道在给定目录中是否创建了新文件。我有下一个代码:

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
private static void CreateWatcher()
    {
        //Create a new FileSystemWatcher.
        FileSystemWatcher watcher = new FileSystemWatcher();

        //Set the filter to all files.
        watcher.Filter ="*.*";

        //Subscribe to the Created event.
        watcher.Created += new FileSystemEventHandler(watcher_FileCreated);

        //Set the path
        watcher.Path = path;

        //Enable the FileSystemWatcher events.
        watcher.EnableRaisingEvents = true;
    }

    private static void watcher_FileCreated(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine("Nuevo archivo creado ->" + e.Name);        
    }

    public static void Main(string[] args)
    {
        CreateWatcher();
        CreateFiles();
        Console.Read();
    }

CreatedFiles() 函数中,我在路径上创建了三个新文件(一个 zip 和两个 txt)。它检测到两个 txt 文件,但与 zip 文件不同。我该如何解决?


建议你看看这里。之前已解决此问题。

使用 FileSystemWatcher 监控目录