C# Program to Trap Events from File
Posted by Superadmin on August 13 2022 08:20:39

C# Program to Trap Events from File

 

 

This C# Program to Trap Events from File. Here the events that happen in the specified locations are trapped.

 

Here is source code of the C# Program to Trap Events from File. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

  1. /*
  2.  * C# Program to Trap Events from File
  3.  */
  4. using System;
  5. using System.IO;
  6. class Test 
  7. {
  8.     static void namechang(object sender, RenamedEventArgs evn) 
  9.     {
  10.         Console.WriteLine("{0} NameChanged to {1}", evn.OldFullPath, evn.FullPath);
  11.     }
  12.     static void changed(object sender, FileSystemEventArgs evn) 
  13.     {
  14.         Console.WriteLine(evn.FullPath + " " + evn.ChangeType);
  15.     }
  16.     static void Main(string[] arg) 
  17.     {
  18.         FileSystemWatcher w = new FileSystemWatcher();
  19.         w.Path = "d:\\srip";
  20.         w.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName |
  21.                          NotifyFilters.LastAccess | NotifyFilters.LastWrite;
  22.         w.Filter = "";
  23.         w.Created += new FileSystemEventHandler(changed);
  24.         w.Deleted += new FileSystemEventHandler(changed);
  25.         w.Changed += new FileSystemEventHandler(changed);
  26.         w.Renamed += new RenamedEventHandler(namechang);
  27.         w.EnableRaisingEvents = true;
  28.         Console.WriteLine("Press any key to quit");
  29.         Console.Read();
  30.     }
  31.  }

Here is the output of the C# Program:

Press any key to quit