How can I monitor a Windows directory for changes?

后端 未结 7 1989
感情败类
感情败类 2020-11-29 22:32

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 whe

7条回答
  •  佛祖请我去吃肉
    2020-11-29 22:57

    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
        }
    

提交回复
热议问题