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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

advertisement
advertisement
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.