How to use FileSystemWatcher in .Net

By Kalit Sikka
Access over 40 UI widgets with everything from interactive menus to rich charts.

This class can use to track for filesystem changes on the local machine, a networked drive, and even a remote machine. Generally use in window services to track changes in background.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace PubishApps
{
    public class ClsFileSystemWatcher
    {
    
/************************************************
* Topic : How to use FileSystemWatcher in .Net
* Reference Required: System.IO, System.Threading.
* Author : kalit sikka
* Usage: This class can use to track for filesystem changes on the local machine, a networked drive, and even a remote machine.
* Generally use in window services to track changes in background.
* For : http://eggheadcafe.com
* **********************************************/

    public  void FileWatcher(string InputDir)
    {    
     using (FileSystemWatcher fsw = new FileSystemWatcher( ))
    {
        fsw.Path = InputDir; // Input Path
        fsw.Filter = @"*.txt"; // Filter for files
        fsw.IncludeSubdirectories = true;

        fsw.NotifyFilter = NotifyFilters.FileName |
            NotifyFilters.Attributes  |
            NotifyFilters.LastAccess  |
            NotifyFilters.LastWrite   |
            NotifyFilters.Security    |
            NotifyFilters.Size        |
            NotifyFilters.CreationTime|
            NotifyFilters.DirectoryName;

        fsw.Changed += new FileSystemEventHandler(OnChanged);
        fsw.Created += new FileSystemEventHandler(OnCreated);
        fsw.Deleted += new FileSystemEventHandler(OnDeleted);
        fsw.Renamed += new RenamedEventHandler(OnRenamed);
        fsw.Error += new ErrorEventHandler(OnError);

        fsw.EnableRaisingEvents = true;

        string strOldFile = InputDir + "OldFile.txt";
        string strNewFile = InputDir + "CreatedFile.txt";

        // Making changes in existing file
        
         using (FileStream stream = File.Open(strOldFile, FileMode.Append))
        {
            StreamWriter sw = new StreamWriter(stream);
             sw.Write("Appending new line in Old File");
            sw.Flush();
            sw.Close();
            
        }

         // Writing new file on FileSystem
        
         using (FileStream stream = File.Create(strNewFile))
        {
            StreamWriter sw = new StreamWriter(stream);
             sw.Write("Writing First line into the File");
            sw.Flush();
            sw.Close();
        }

         File.Delete(strOldFile);
         File.Delete(strNewFile);

         
         // Minimum time given to event handler to track new events raised by the filesystem.
      
         Thread.Sleep(1000);
    }
}

    public static void OnChanged(object source, FileSystemEventArgs e)
    {
         Console.WriteLine("File " + e.FullPath + " :" + e.ChangeType);
    }

     public static void OnDeleted(object source, FileSystemEventArgs e)
    {
         Console.WriteLine("File " + e.FullPath + " :" + e.ChangeType);
    }

     public static void OnCreated(object source, FileSystemEventArgs e)
    {
         Console.WriteLine("File " + e.FullPath + " :" + e.ChangeType);
    }

     public static void OnRenamed(object source, RenamedEventArgs e)
    {
         Console.WriteLine("File " + e.OldFullPath + " [Changed to] " + e.FullPath);
    }

    public static void OnError(object source, ErrorEventArgs e)
    {
         Console.WriteLine("Error " + e.ToString( ));
    }
         
        
        
         static void Main(string[] args)
        {
            ClsFileSystemWatcher FSysWatcher = new ClsFileSystemWatcher();
             FSysWatcher.FileWatcher(@"D:\kalit\");
         }
    
    
    }

     


}


Popularity  (4400 Views)