FileSystemEventArgs: Changed, Created, Deleted. : File System « File Stream « C# / C Sharp






FileSystemEventArgs: Changed, Created, Deleted.

 
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        FileSystemWatcher fsw = new FileSystemWatcher("C:\\");
        fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
            | NotifyFilters.FileName |NotifyFilters.DirectoryName;

        fsw.Changed += new FileSystemEventHandler(OnChanged);
        fsw.Created += new FileSystemEventHandler(OnChanged);
        fsw.Deleted += new FileSystemEventHandler(OnChanged);
        fsw.Renamed += new RenamedEventHandler(OnRenamed);
        fsw.Error += new ErrorEventHandler(OnError);
        fsw.EnableRaisingEvents = true;
        Console.WriteLine("Press Enter to quit the sample.");
        Console.ReadLine();
    }
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        WatcherChangeTypes wct = e.ChangeType;
        Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString());
    }
    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        WatcherChangeTypes wct = e.ChangeType;
        Console.WriteLine("File {0} {2} to {1}", e.OldFullPath, e.FullPath, wct.ToString());
    }
    private static void OnError(object source, ErrorEventArgs e)
    {
        Console.WriteLine("error");
        if (e.GetException().GetType() == typeof(InternalBufferOverflowException))
        {
            Console.WriteLine(("internal buffer overflow: " + e.GetException().Message));
        }
    }

}

   
  








Related examples in the same category

1.File System Watcher DemoFile System Watcher Demo
2.FileSystem Watcher
3.FileSystemInfo Class provides the base class for both FileInfo and DirectoryInfo objects.
4.FileSystemInfo.LastAccessTime Property gets or sets the time the current file or directory was last accessed.