I have this program that monitors multiple directories (watch for files when deleted, then get their timestamp).. I was able to do this 'dynamically' where it can be 'x' number of directories (at the time the program starts)...

The problem is that when I call StopWatching, and then I say delete a file from any of the directories, FileSystemWatcher seems to still be "watching". I wanted FileSystemWatcher to be ON, then OFF, then ON again at certain parts of the program.. for now, I can't make it to where it's OFF.

Thanks in advance


I have this method:

static FileSystemWatcher watcher = new FileSystemWatcher();

static void Main(string[] args)
{
.
.
<then I call the method StartWatching here>
.
.
<then some processes and other methods called here, etc>
.
.
<then somewhere in the code i would need Filesystemwatcher to stop watching the directories>
<I call this: StopWatching>
.
.
<then somewhere in the code (again), I would need Filesystemwatcher to start watching>
.
.


}



Here are the methods:

StartWatching

static public void StartWatching()
{
string sourceFolderToWatch = "";

for (int i = 0; i < sourceFolderList.Count; i++)
{
sourceFolderToWatch = (string)sourceFolderList[i];
watcher = new FileSystemWatcher();
watcher.Path = @sourceFolderToWatch;
watcher.Filter = "*.tif";
watcher.EnableRaisingEvents = true;
watcher.Deleted += new FileSystemEventHandler(watcherDeleted);
}

}

static public void StopWatching()
{
string sourceFolderToWatch = "";

for (int i = 0; i < sourceFolderList.Count; i++)
{
sourceFolderToWatch = (string)sourceFolderList[i];
watcher = new FileSystemWatcher();
watcher.Path = @sourceFolderToWatch;
watcher.Filter = "*.tif";
watcher.EnableRaisingEvents = false;
}

}



static void watcherDeleted(object sender, FileSystemEventArgs e)
{
anyFileDeleted = true;

// the value for deleteTime only changes if theres a delete that happened
referenceTime = DateTime.Now;
Console.WriteLine("******FILE DELETED******");
Console.WriteLine("This file was deleted: " + e.Name);
Console.WriteLine(referenceTime);
}