Build Errors on C# Console App

  • Thread starter Thread starter dm_dal
  • Start date Start date
D

dm_dal

I'm trying to compile the FileSystemWatcher example on MSDN but it won't
compile and gives the following build error:

1) An object reference is required for the nonstatic field, method, or
property 'FileWatcherDaemon.fileWatcher.OnChanged(object,
System.IO.FileSystemEventArgs)'

This is given on the following line:

watcher.Changed += new FileSystemEventHandler(Changed);
 
dm_dal said:
I'm trying to compile the FileSystemWatcher example on MSDN but it won't
compile and gives the following build error:

1) An object reference is required for the nonstatic field, method, or
property 'FileWatcherDaemon.fileWatcher.OnChanged(object,
System.IO.FileSystemEventArgs)'

This is given on the following line:

watcher.Changed += new FileSystemEventHandler(Changed);

The above line doesn't exist in the sample - it's OnChanged, not
Changed.

I've just cut and paste the original from MSDN, and it compiles fine.
Which version of the MSDN are you using?
 
Must have been a typo in my first post.
Here's all the code. But I still get the same error.
using System;
using System.IO;

namespace FileWatcherDaemon
{
/// <summary>
/// Summary description for fileWatcher.
/// </summary>
public class fileWatcher
{

public static void Main()
{
string[] args = System.Environment.GetCommandLineArgs();
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = "C:\\Temp";
watcher.Filter = "*.txt";
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.EnableRaisingEvents = true;
watcher.Changed += new FileSystemEventHandler(OnChanged);
Console.WriteLine("Press \'q\' to Quit");
while(Console.Read()!='q');
}

private void OnChanged(object sender, FileSystemEventArgs e)
{
Console.WriteLine("File Changed");
Console.WriteLine("file: " + e.Name.ToString());
}
}
}
 
Back
Top