class Program
{
FileSystemWatcher _watchFolder;
string sPath = @"D:\TestMonitor";
static void Main(string[] args)
{
Program p = new Program();
Thread t = new Thread(new ThreadStart(p.startActivityMonitoring));
t.Start();
}
private void startActivityMonitoring()
{
_watchFolder = new FileSystemWatcher();
_watchFolder.Path = Convert.ToString(sPath);
_watchFolder.NotifyFilter = System.IO.NotifyFilters.DirectoryName;
_watchFolder.NotifyFilter =
_watchFolder.NotifyFilter | System.IO.NotifyFilters.FileName;
_watchFolder.NotifyFilter =
_watchFolder.NotifyFilter | System.IO.NotifyFilters.Attributes;
_watchFolder.Changed += new FileSystemEventHandler(eventRaised);
_watchFolder.Created += new FileSystemEventHandler(eventRaised);
_watchFolder.Deleted += new FileSystemEventHandler(eventRaised);
_watchFolder.Renamed += new System.IO.RenamedEventHandler(eventRaised);
_watchFolder.EnableRaisingEvents = true;
}
private void eventRaised(object sender, System.IO.FileSystemEventArgs e)
{
switch (e.ChangeType)
{
case WatcherChangeTypes.Changed:
Console.WriteLine(string.Format("File {0} has been modified\r\n", e.FullPath));
break;
case WatcherChangeTypes.Created:
Console.WriteLine(string.Format("File {0} has been created\r\n", e.FullPath));
break;
case WatcherChangeTypes.Deleted:
Console.WriteLine(string.Format("File {0} has been deleted\r\n", e.FullPath));
break;
default: // Another action
break;
}
}
}
A simple program using FileSystemWatcher to poll for changes inside a directory when i try to log the changes using Console.WriteLine it does not work though.
Not sure what is causing this issue since Console.WriteLine works well inside any thread
Your program is exiting right after it starts the thread. You need to keep the program running.
One simple way would to be include a Console.ReadLine to stop the program from exiting.