I'm creating a List of FileSystemWatchers.
List<ExtSystemFileWatcher> fswMonitors = new List<FileSystemWatcher> ();
The number of them in the list is dynamic depending on the user. This is done from a INI file and an array of Monitor objects from my own Monitor class are created. The class simply has varibles like the Montior number, Path to monitor, Ext to look for etc.
if (iNumberMonitors > 0)
{
obMonitors = ReadMonitors(iNumberMonitors);
for (int iCounter = 0; iCounter < iNumberMonitors; iCounter++)
{
FileSystemWatcher fswCurrent = new FileSystemWatcher();
fswCurrent.Path = obMonitors[iCounter].strMonPath;
fswCurrent.EnableRaisingEvents = true;
fswCurrent.NotifyFilter = NotifyFilters.FileName;
fswCurrent.Filter = "*." + obMonitors[iCounter].strMonExt;
fswCurrent.Deleted += OnDelete;
fswMonitors.Add(fswCurrent);
}
}
In the 'OnDelete' Method that each FileSystemWatcher calls if the Delete event fires I need to know which of the FileSystemWatchers is calling it.
My question is how can I know which FileSystemMonitor in the List is calling the method?
Do you need anything else more than just checking sender
in your eventHandler?
private void OnDelete(object sender, ...)
{
var watcher = (FileSystemWatcher) sender;
// probably list.IndexOf here if you really need an index
}