windowsdirectorymonitoring

How can I monitor a Windows directory for changes?


When a change is made within a directory on a Windows system, I need a program to be notified immediately of the change.

Is there some way of executing a program when a change occurs?

I'm not a C/C++/.NET programmer, so if I could set up something so that the change could trigger a batch file then that would be ideal.


Solution

  • Use a FileSystemWatcher like below to create a WatcherCreated Event().

    I used this to create a Windows Service that watches a Network folder and then emails a specified group on arrival of new files.

        // Declare a new FILESYSTEMWATCHER
        protected FileSystemWatcher watcher;
        string pathToFolder = @"YourDesired Path Here";
    
        // Initialize the New FILESYSTEMWATCHER
        watcher = new FileSystemWatcher {Path = pathToFolder, IncludeSubdirectories = true, Filter = "*.*"};
        watcher.EnableRaisingEvents = true;
        watcher.Created += new FileSystemEventHandler(WatcherCreated);
    
        void WatcherCreated(object source , FileSystemEventArgs e)
        {
          //Code goes here for when a new file is detected
        }