Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know which FileSystemWatcher is calling Method?

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?

like image 314
MarkBrad Avatar asked Jan 17 '26 23:01

MarkBrad


1 Answers

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
}
like image 131
Snowbear Avatar answered Jan 19 '26 17:01

Snowbear



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!