使用FileWatcher检测文件夹中是否有新文件

时间:2015-11-11 15:25:11

标签: 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();
    }

In" CreatedFiles()"功能,我在路径上创建3个新文件(1个zip和2个txt)。它检测到我的2个txt文件,但与zip文件不一样。我该如何解决?

谢谢!

1 个答案:

答案 0 :(得分:1)

建议你看看这里。这已经在以前解决了。

Using FileSystemWatcher to monitor a directory