FileSystemWatcher Demo : FileSystemWatcher « File Directory Stream « C# / CSharp Tutorial






using System;
using System.IO;

public class MainClass
{
    public static void Main(string[] args)
    {
         FileSystemWatcher watcher = new FileSystemWatcher();
         watcher.Path = @"c:\Test";
         watcher.NotifyFilter  = NotifyFilters.LastAccess |
                                 NotifyFilters.LastWrite |
                                 NotifyFilters.FileName |
                                 NotifyFilters.DirectoryName;
         watcher.Filter = "*.txt";
         watcher.Changed += new FileSystemEventHandler(OnChanged);
         watcher.Created += new FileSystemEventHandler(OnChanged);
         watcher.Deleted += new FileSystemEventHandler(OnChanged);
         watcher.Renamed += new RenamedEventHandler(OnRenamed);
         watcher.EnableRaisingEvents = true;
                     
    }
    
    public static void OnChanged(object source, FileSystemEventArgs e)
    {
           Console.WriteLine("Event Fired");
    }
    public static void OnRenamed(object source, RenamedEventArgs e)
    {
        Console.WriteLine("Event Fired");
    }

}








15.36.FileSystemWatcher
15.36.1.Use FileSystemWatcher to detect file changes
15.36.2.Watching for filesystem updates synchronous
15.36.3.FileSystemWatcher Demo
15.36.4.File Watch
15.36.5.My Directory Watcher