开发者

How to know which FileSystemWatcher is calling Method?

开发者 https://www.devze.com 2023-03-28 21:13 出处:网络
I\'m creating a List of FileSystemWatchers. List<ExtSystemFileWatcher> fswMonitors = new List<FileSystemWat开发者_如何转开发cher> ();

I'm creating a List of FileSystemWatchers.

List<ExtSystemFileWatcher> fswMonitors = new List<FileSystemWat开发者_如何转开发cher> ();

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
}


You can use a closure where you hook your "Delete event" instead of passing the method itself:

fswCurrent.Deleted += OnDelete;

you pass something like

fswCurrent.Deleted += (sender, e) => OnDelete(sender, e, iCounter)

of course you will need to change the signature of OnDelete to take the additional Index. After reading your comment you might don't need it though, as the other answers suggests.


The event handler has a sender parameter that is a reference to the FileSystemWatcher that raised the event.

private static void OnDeleted(object source, FileSystemEventArgs e)
{
    FileSystemWatcher watcher = source as FileSystemWatcher;
    if(watcher != null)
    {
        string deletedFile = e.FullPath;
        //Update db with watcher and deletedFile
    }
}


"sender" parameter in OnDelete method will point to the originator of the event

void OnDelete(object sender, EventArgs e)
{
    var watcher = ((FileSystemWatcher) sender);
    .....
}


Just cast the sender parameter of the event handler which you attach to the Deleted event.

For example:

class Program
{
    static void Main(string[] args)
    {
        var watcher = new FileSystemWatcher { Path = @"c:\temp", Filter = "*.txt" };
        watcher.Deleted += watcher_Deleted;
        watcher.EnableRaisingEvents = true;
        Console.ReadLine();
    }

    static void watcher_Deleted(object sender, FileSystemEventArgs e)
    {
        var watcher = sender as FileSystemWatcher;
    }
}


May be check if each of these pooled objects might have an hashcode in it. Which should be unique.

0

精彩评论

暂无评论...
验证码 换一张
取 消