Use FileSystemWatcher to detect file changes : FileSystemWatcher : File Directory Stream C# Examples


C# Examples » File Directory Stream » FileSystemWatcher »

 

Use FileSystemWatcher to detect file changes









    
using  System;
using  System.IO;
using  System.Windows.Forms;

static  class  MainClass
{
        static  void  Main()
        {
                using  (FileSystemWatcher  watch  =  new  FileSystemWatcher())
                {
                        watch.Path  =  Application.StartupPath;
                        watch.Filter  =  "*.*";
                        watch.IncludeSubdirectories  =  true;

                        watch.Created  +=  new  FileSystemEventHandler(OnCreatedOrDeleted);
                        watch.Deleted  +=  new  FileSystemEventHandler(OnCreatedOrDeleted);
                        watch.EnableRaisingEvents  =  true;

                        if  (File.Exists("test.bin"))
                        {
                                File.Delete("test.bin");
                        }

                        using  (FileStream  fs  =  new  FileStream("test.bin",  FileMode.Create))
                        {
                        }
                }
        }

        private  static  void  OnCreatedOrDeleted(object  sender,  FileSystemEventArgs  e)
        {
                Console.WriteLine("\tNOTIFICATION:  "  +  e.FullPath  +  "'  was  "  +  e.ChangeType.ToString());
        }
}
    
   
  
   



Output

NOTIFICATION: C:\Java_Dev\WEB\dev\CSharp\test.bin' was Deleted


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo File Directory Stream
» FileSystemWatcher