[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 939
  • Last Modified:

C#.net FileSystemWatcher Events and Passing

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'?
0
Rocker4500
Asked:
Rocker4500
  • 2
1 Solution
 
sstopsCommented:
Hi,

the triggering FSW should be the sender object. You should be able to access the notifyfilter by using the following code:
 ((System.IO.FileSystemWatcher)sender).NotifyFilter.ToString().

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

Regards
Sascha
0
 
Rocker4500Author Commented:
LEGEND!!!!
Thank you very much, i never thought of that. took me ages!
Cheers
0
 
daveamourCommented:
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)
        {
            Console.WriteLine(e.FullPath);
        }
    }
 
    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)
        {
            Console.WriteLine(((FileSystemWatcher)sender).NotifyFilter.ToString());
        }
    }
}

Open in new window

0
 
daveamourCommented:
Ooop too slow!
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now