Banner

FileWatcher

C#

/*
    Michael Eaton
    Sun Sep 01 2002 11:49:29
    Purpose: To set a FileSystem watch on the incoming logs
    from my router.  As the log file changes, a message will
    be printed to the console.
*/

using System;
using System.IO;

public class watchIncoming {
    public static void Main() {
        FileSystemWatcher incoming = new FileSystemWatcher();
        incoming.Path = @"c:\program files\linksys\LogViewer\";
        incoming.NotifyFilter = NotifyFilters.LastAccess |
                                NotifyFilters.LastWrite |
                                NotifyFilters.FileName |
                                NotifyFilters.DirectoryName;
        incoming.Filter = "incoming.log";

        incoming.Changed += new FileSystemEventHandler(OnChanged);

        incoming.EnableRaisingEvents = true;

        Console.WriteLine("Press \'q\' to quit.");
        while(Console.Read()!='q');
    }

    public static void OnChanged(object source, FileSystemEventArgs e) {
        Console.WriteLine("{0} : Changed", System.DateTime.Now);
    }
}
csharpindex.com/colorCode
Change the hard-coded paths and the filter to something appropriate for your system.
Save as 'watcher.cs'.
Compile at the command-line using: 'csc watcher.cs'
Back to the samples...