Search code examples
c#.netfilesystemwatcher

How to watch all the folders of a specific name under a parent directory with FileSystemWatcher?


I have a directory structure like the following where csv files may be added to any of the sub-directories. (The business logic is that order files firstly will be save under "Vendor" folder, and after being checked, they will be moved to "Processed" folders.)

I want to only monitor folders called "Processed". For example, if there are files added to "Processed" folder, I would like to get notified and do something in the callback methods. If files are added under "Vendor" folder, I want to ignore them. How should I configure FileSystemWatcher to achieve this?

enter image description here

This is what I've got now.

public static void Watch()
{
   FileSystemWatcher watcher = new FileSystemWatcher();
   watcher.Path = path; //here is the path of the "Order" folder;
   watcher.Created += FileSystemWatcher_Created;
   watcher.EnableRaisingEvents = true;         
}

private static void FileSystemWatcher_Created(object source, FileSystemEventArgs e)
{
   //do something when there are new files added to the watched directory
}

Solution

  • FileSystemWatcher has a property called IncludeSubdirectories, When true, IncludeSubdirectories is recursive through the entire sub tree, not just the immediate child directories.

    So an example would be something like this (modified MSDN example):

    private static void Watch(string watch_folder)
    {
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.IncludeSubdirectories = true;
        watcher.Path = watch_folder;
        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
    
        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);
    
        // Begin watching.
        watcher.EnableRaisingEvents = true;
    }
    
    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
    }
    
    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        // Specify what is done when a file is renamed.
    }
    

    See also

    Update

    This is a small console example on how to filter notifications to only react to the Processed folders based on @MickyD comment:

    class Program
    {
        static void Main(string[] args)
        {
    
            try
            {
                string path = Path.Combine(Directory.GetCurrentDirectory(), "Order");
                Console.WriteLine(path);
                FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(path);
                fileSystemWatcher.IncludeSubdirectories = true;
                fileSystemWatcher.Changed += FileSystemWatcher_Changed;
                fileSystemWatcher.EnableRaisingEvents = true;
    
                Process.GetCurrentProcess().WaitForExit();
                fileSystemWatcher.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    
        private static void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
        {
            if (e.Name.Contains("Processed"))
                Console.WriteLine("Processed folder has changed");
        }
    }