How to monitor a directory in C#
In C#, you can use the FileSystemWatcher
class to monitor a directory for changes. Here is an example of how to use the FileSystemWatcher
class to monitor a directory and handle changes:
using System;
using System.IO;class DirectoryWatcher
{
static void Main()
{
// Set up the FileSystemWatcher object
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\Temp";
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.Filter = "*.*";// Add event handlers for the events you want to monitor
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);// Start monitoring the directory
watcher.EnableRaisingEvents = true;// Wait for user to quit program
Console.WriteLine("Press 'q' to quit.");
while (Console.Read() != 'q') ;
}// Define the event handlers
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine($"File {e.FullPath} {e.ChangeType}");
}private static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine($"File {e.OldFullPath} renamed to {e.FullPath}");
}
}
In this example, the FileSystemWatcher
object is set up to monitor the C:\Temp
directory for changes to any file. The NotifyFilter
property is set to LastWrite
and FileName
, which means that the watcher will only notify you if a file is changed or if a new file is created. The Filter
property is set to *.*
to watch for all files.
The Changed
, Created
, Deleted
, and Renamed
events are handled by the event handlers OnChanged
and OnRenamed
. These event handlers simply print out a message to the console indicating what file was changed and what type of change occurred.
Finally, the EnableRaisingEvents
property is set to true
to start monitoring the directory. The program waits for the user to press 'q' before exiting.
Share This with your friend by choosing any social account
You may also read following recent Post
![]() |
How old are you and what is your net worth? Also, what is your educational background and what sort of a job do you do? Do you enjoy your work?
16 By Naumanshafi |
![]() |
create appointment booking page with available time slot using database ms sql server
29 By Junaid A |
![]() |
what is Asp.Net Core
43 By Junaid A |
![]() |
publish has encountered an error Object reference not set to an instance of an object A diagnostic log has been written to following location
58 By Junaid A |
![]() |
The provided URI scheme 'http' is invalid; expected 'https'." & vbCrLf & "Parameter name: via
76 By Junaid A |
![]() |
C# Language Basics
1580 By |
![]() |
Crud in Asp.NET using tabs
601 By Haider |
![]() |
Parser Error
667 By Usman Jafar |
![]() |
What are 3 C
160 By |
![]() |
what is .net
388 By |
![]() |
How to post uploaded file and form data in MVC using jquery?ts Common scenario where you want to pos
196 By |
![]() |
MVC url routing
231 By |
![]() |
Learn about Session
259 By |