Solved FileSystemWatcher Events and Passing

Posted on 2009-02-21
Last Modified: 2013-12-17
I hope this explanation explains things and is understandable!

I have a program, at runtime a 2D array of SystemFileWatchers are created, all works perfect, events are triggered etc.

I have 4 FileWatcher events:

                        fw[i, y].Changed += fwchanged;
                        fw[i, y].Deleted += fwdeleted;
                        fw[i, y].Renamed += fwrenamed;
                        fw[i, y].Created += fwcreated;

Which trigger the events, as the example bellow:

private void fwchanged(object sender, System.IO.FileSystemEventArgs e)

            trw.writetolog(e.ChangeType + ": " + e.FullPath, "DirMonLog");

This is all ok.

However, i want to addapt this and I am im not sure how and if it is even possible!
What i want to achive is the ability to access the FileWatchers NotifyFilter with in the event -  such as - private void fwchanged(object sender, System.IO.FileSystemEventArgs e) - for example.  

In order to do this i want to pass the NotifyFilter as a string to the event in a way like the half pesudo code bellow:

                        convert fw[i,y].NotifyFilter to string (if required)
                        fw[i, y].Changed += fwchanged(string notifyfilter);

private void fwchanged(object sender, System.IO.FileSystemEventArgs e, string notifyfilter)

In order to do such if i am defining what i wish to pass i must also define the 'sender' and 'System.IO.FileSystemEventArgs':

fw[i, y].Changed += fwchanged(sender, System.IO.FileSystemEventArgs, string notifyfilter);

My question is: How do i pass the 'System.IO.FileSystemEventArgs'?  I can pass the sender by simply using this...i think?  but i am unsure of how and if it is even possible to pass the 'System.IO.FileSystemEventArgs'?
Question by:Rocker4500
    LVL 2

    Accepted Solution


    the triggering FSW should be the sender object. You should be able to access the notifyfilter by using the following code:

    I think this should be much easier than adding to the event handler a separate string.

    LVL 1

    Author Closing Comment

    Thank you very much, i never thought of that. took me ages!
    LVL 19

    Expert Comment

    Ok if I understand your question correctly then the object sender is the instance of the FileSystemWatcher raising the event so just cast that from object to FileSystemWatcher to get at the NotifyFile - see attached example
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace EEGenericConsoleApp
        class Watcher
            static void Main()
                WatcherClass watcher = new WatcherClass();
                while (true)
            static void watcher_Created(object sender, FileSystemEventArgs e)
        public class WatcherClass
            FileSystemWatcher watcher;
            public WatcherClass()
                watcher = new FileSystemWatcher(@"E:\");
                watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                watcher.Filter = "*.txt";
                watcher.Created += new FileSystemEventHandler(watcher_Created);
                watcher.EnableRaisingEvents = true;
            private void watcher_Created(object sender, FileSystemEventArgs e)

    Open in new window

    LVL 19

    Expert Comment

    Ooop too slow!

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now