c# - 使用 FileWatcher 检测文件夹中是否创建了新文件

标签 c# file directory filesystemwatcher

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

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 文件不同。我该如何解决?

最佳答案

建议你看这里。之前已经解决了这个问题。

Using FileSystemWatcher to monitor a directory

关于c# - 使用 FileWatcher 检测文件夹中是否创建了新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33653841/

相关文章:

batch-file - 批处理文件如何在目录的全部内容上运行?

c++ - 带目录的 Makefile

c# - 使用 System.Data.OracleClient 配置 Fluent NHibernate

c# - 命名空间 B 中的元素 A 在命名空间 D 中具有无效的子元素 C

c - fwrite后在txt文件上显示汉字

file - Windows 按名称批量移动(或复制)PDF 文件到相应文件夹

c - 反转数组[更新],文件扫描

c# - List.Sort(自定义排序...)

c# - 如何使用 Visual Studio 2015 注册 .NET Core 1.1 RTM 的自定义项模板?

linux - 在 Ubuntu 中按时间顺序展开和搜索?