0

I apologize if this is a pretty simple fix, as I am new to C#. Here is a code snippet for monitoring a folder in Windows:

FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = label1.Text;

            watcher.NotifyFilter = NotifyFilters.LastWrite |
                                   NotifyFilters.LastAccess |
                                   NotifyFilters.FileName |
                                   NotifyFilters.DirectoryName;
"
            watch.Filter = "*.*";

            watcher.Created += new FileSystemEventHandler(OnChanged(label4));
            watcher.EnableRaisingEvents = true;

As you can see, while I'm monitoring a folder for files being "Created", for each file it will execute the method OnChanged.

I have a label on my .net App, and I would like to pass that label text as a string variable into the OnChanged method. My question is how do I pass in a variable into the OnChanged method? It appears whatever syntax I have tried, including the above syntax Visual Studio 2010 is not liking it.

The onChanged method looks like this:

            public static void OnChanged(object source, FileSystemEventArgs e, String label4)
            {
                FileInfo file = new FileInfo(e.FullPath);
                String fileName = file.Name;
                String outputPath = label4.Text + file.Name;
            }
adbarads
  • 1,253
  • 2
  • 20
  • 43

2 Answers2

1

You can not pass your argument directly, but you can use a lambda expression to wrap it.

watcher.Created += (source, e) => OnChanged(source, e, label4);
DDoSolitary
  • 328
  • 2
  • 13
0

You can use a lambda expression for this:

watcher.Created += (sender, args) => OnChanged(label)

Some background on lambda expressions

hkf
  • 4,440
  • 1
  • 30
  • 44