简体   繁体   中英

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

I'm trying to know if there are new files created in a given directory. I have next code:

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() function, I'm creating on the path three new files (one zip and two txt). It detects two txt files but not the same with the zip file. How can I solve that?

Suggest you take a look here. This has been addressed previously.

Using FileSystemWatcher to monitor a directory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM