How can I monitor a Windows directory for changes?

后端 未结 7 1954
感情败类
感情败类 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
        }
    
    0 讨论(0)
  • 2020-11-29 22:58

    If you want something non-programmatic try GiPo@FileUtilities ... but in that case the question wouldn't belong here!

    0 讨论(0)
  • 2020-11-29 23:00

    There is no utility or program the comes with Windows to do it. Some programming required.

    As noted in another answer, .NET's FileSystemWatcher is the easiest approach.

    The native API ReadDirectoryChangesW is rather harder to use (requires an understanding of completion ports).

    0 讨论(0)
  • 2020-11-29 23:01

    Use a FileSystemWatcher

    0 讨论(0)
  • 2020-11-29 23:01

    I came on this page while searching for a way to monitor filesystem activity. I took Refracted Paladin's post and the FileSystemWatcher that he shared and wrote a quick-and-dirty working C# implementation:

    using System;
    using System.IO;
    
    namespace Folderwatch
    {
        class Program
        {
            static void Main(string[] args)
            {
    
                //Based on http://stackoverflow.com/questions/760904/how-can-i-monitor-a-windows-directory-for-changes/27512511#27512511
                //and http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
    
                string pathToFolder = string.Empty;
                string filterPath = string.Empty;
                const string USAGE = "USAGE: Folderwatch.exe PATH FILTER \n\n e.g.:\n\n Folderwatch.exe c:\\windows *.dll";
    
                try
                {
                    pathToFolder = args[0];
    
                }
                catch (Exception)
                {
                    Console.WriteLine("Invalid path!");
                    Console.WriteLine(USAGE);
                    return;
                }
    
                try
                {
                    filterPath = args[1];
                }
                catch (Exception)
                {
                    Console.WriteLine("Invalid filter!");
                    Console.WriteLine(USAGE);
                    return;
    
                }
    
                FileSystemWatcher watcher = new FileSystemWatcher();
    
                watcher.Path = pathToFolder;
                watcher.Filter = filterPath;
    
                watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | 
                    NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | 
                    NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
    
                // 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;
    
                // Wait for the user to quit the program.
                Console.WriteLine("Monitoring File System Activity on {0}.", pathToFolder);
                Console.WriteLine("Press \'q\' to quit the sample.");
                while (Console.Read() != 'q') ;
    
            }
    
            // Define the event handlers. 
            private static void OnChanged(object source, FileSystemEventArgs e)
            {
                // Specify what is done when a file is changed, created, or deleted.
                Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
            }
    
            private static void OnRenamed(object source, RenamedEventArgs e)
            {
                // Specify what is done when a file is renamed.
                Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
            }
        }
    }
    

    To use this, download Visual Studio (Express will do). The create a new C# console application called Folderwatch and copy and paste my code into your Program.cs.

    As an alternative you could use Sys Internals Process Monitor: Process Monitor It can monitor the file system and a bunch more.

    0 讨论(0)
  • 2020-11-29 23:06

    This question helped me a lot to understand the File Watcher System. I implemented ReadDirectoryChangesW to monitor a directory and all its sub directories and get the information about change in those directories.

    I have written a blog post on the same, and I would like to share it so it may help someone who land up here for the same problem.

    Win32 File Watcher Api to monitor directory changes.

    0 讨论(0)
提交回复
热议问题