Archive of: FileSystem

  1. Using .NET to Monitor a Directory for Changes posted February 8, 2010

    .NET’s FileSystemWatcher class can soothe the paranoid control freak in all of us by monitoring a specified folder for different types of file system changes.

    The Code

    using System;
    using System.Text;
    using System.IO;
    
    namespace FolderWatcher {
      class Watcher {
        static void Main(string[] args) {
          FileSystemWatcher watcher = new FileSystemWatcher(@"D:\test");
          watcher.IncludeSubdirectories = true;
          watcher.Filter = "";
          watcher.Renamed += new RenamedEventHandler(renamed);
          watcher.Deleted += new FileSystemEventHandler(changed);
          watcher.Changed += new FileSystemEventHandler(changed);
          watcher.Created += new FileSystemEventHandler(changed);
          watcher.EnableRaisingEvents = true;
    
          Console.ReadKey();
       }
    
        private static void renamed(object sender, RenamedEventArgs e) {
          Console.WriteLine(DateTime.Now + ": " +
              e.ChangeType + " " + e.FullPath);
        }
    
        private static void changed(object sender, FileSystemEventArgs e) {
          Console.WriteLine(DateTime.Now + ": " +
              e.ChangeType + " " + e.FullPath);
        }
      }
    }
    

    Explanation

    FileSystemWatcher watcher = new FileSystemWatcher(@"D:\test");
    

    The core of is application is .NET’s FileSystemWatcher class which monitors directories for file system changes.

    watcher.IncludeSubdirectories = true;
    watcher.Filter = "";
    

    Tells the FileSystemWatcher object to include changes in subdirectories and raise events on every type of file.

    An empty string for the filter will match every file and folder in the watched directory. File types and names can also be added to the filter to… Read the rest

.NET Algorithmic Art ASP ASP .NET ASP .NET MVC audio Bill Gates C# code review Computational Art CouchDB CSS Database DateTime debug DevDays eclipse ethics FileSystem Git Improvement iPhone Java JQuery Knopflerfish Layouts mail Microsoft Oracle OSGi Processing Python Ruby SMTP SOA Software Piracy Source Control SQL StackOverflow tools TortoiseGit training Web Standards Windows XHTML