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.
/*
* C# Program to Trap Events from File
*/
using System;
using System.IO;
class Test
{
static void namechang(object sender, RenamedEventArgs evn)
{
Console.WriteLine("{0} NameChanged to {1}", evn.OldFullPath, evn.FullPath);
}
static void changed(object sender, FileSystemEventArgs evn)
{
Console.WriteLine(evn.FullPath + " " + evn.ChangeType);
}
static void Main(string[] arg)
{
FileSystemWatcher w = new FileSystemWatcher();
w.Path = "d:\\srip";
w.NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName |
NotifyFilters.LastAccess | NotifyFilters.LastWrite;
w.Filter = "";
w.Created += new FileSystemEventHandler(changed);
w.Deleted += new FileSystemEventHandler(changed);
w.Changed += new FileSystemEventHandler(changed);
w.Renamed += new RenamedEventHandler(namechang);
w.EnableRaisingEvents = true;
Console.WriteLine("Press any key to quit");
Console.Read();
}
}
Here is the output of the C# Program:
Press any key to quit
Sanfoundry Global Education & Learning Series – 1000 C# Programs.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
If you wish to look at all C# Programming examples, go to 1000 C# Programs.
Next Steps:
- Get Free Certificate of Merit in C# Programming
- Participate in C# Programming Certification Contest
- Become a Top Ranker in C# Programming
- Take C# Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Apply for C# Internship
- Apply for Computer Science Internship
- Practice MCA MCQs
- Practice Computer Science MCQs
- Buy MCA Books