C# Watch file system

By admin - Last updated: Thursday, November 1, 2007 - Save & Share - Leave a Comment
using System.IO;
public static void WatchFolder()
{
    FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
    fileSystemWatcher .Path = "C:\\FoldName";
    fileSystemWatcher .NotifyFilter =
           NotifyFilters.LastAccess | NotifyFilters.LastWrite
            | NotifyFilters.FileName | NotifyFilters.DirectoryName;

    fileSystemWatcher .Filter = "*.cs";

    // Add an event handler.
    oFileSystemWatcher.Created += new FileSystemEventHandler(OnCreated);

    // Begin watching.
    oFileSystemWatcher.EnableRaisingEvents = true;

    Console.WriteLine("Press \'q\' to quit.");

    // Wait for the user to quit the program.
    while(Console.Read()!='q');
}

// The event handler
private static void OnCreated(object source, FileSystemEventArgs e)
{
    Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
}



Posted in Code Notes • Tags: , Top Of Page