Directory Modification Monitoring

前端 未结 4 1158
失恋的感觉
失恋的感觉 2020-12-17 02:31

I\'m building a C# application that will monitor a specified directory for changes and additions and storing the information in a database.

I would like to avoid che

4条回答
  •  被撕碎了的回忆
    2020-12-17 02:52

    Use the FileSystemWatcher object. Here is some code to do what you are looking for.

        // Declares the FileSystemWatcher object
        FileSystemWatcher watcher = new FileSystemWatcher(); 
    
        // We have to specify the path which has to monitor
    
         watcher.Path = @"\\somefilepath";     
    
        // This property specifies which are the events to be monitored
         watcher.NotifyFilter = NotifyFilters.LastAccess |
           NotifyFilters.LastWrite | NotifyFilters.FileName | notifyFilters.DirectoryName; 
        watcher.Filter = "*.*"; // Only watch text files.
    
        // Add event handlers for specific change events...
    
        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.
        }
    

提交回复
热议问题