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

23,138

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

Using FileSystemWatcher to monitor a directory

Share:
23,138
Jaime Menendez Llana
Author by

Jaime Menendez Llana

Updated on July 09, 2022

Comments

  • Jaime Menendez Llana
    Jaime Menendez Llana almost 2 years

    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?

  • moarra
    moarra almost 7 years
    Never put just links...give a detailed answer, remember the link can get broken..
  • Alisson Reinaldo Silva
    Alisson Reinaldo Silva almost 7 years
    If it was addressed previously, then the question should be marked as duplicated instead of receiving this kind of answers.