0

I am trying to monitor a directory for new files in it with the FileSystemWatcher Class.

My Problem is that the event doesn't get triggered. My watcher class is:

public class Filewatcher
{
    private FileSystemWatcher watcher;

    public Filewatcher(string path, string filefilter)
    {
        this.watcher = new FileSystemWatcher();
        this.watcher.Path = path;
        this.watcher.NotifyFilter = NotifyFilters.FileName; //| NotifyFilters.LastWrite | NotifyFilters.LastAccess
        this.watcher.Filter =  filefilter;
        this.watcher.Created += new FileSystemEventHandler(OnChanged);
    }

    public void start_watching()
    {
        //this.watcher.IncludeSubdirectories = true;
        this.watcher.EnableRaisingEvents = true;

        Console.WriteLine("Press Enter to quit\r\n");
        Console.ReadLine(); 
    }

    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        //FileInfo objFileInfo = new FileInfo(e.FullPath);
        //if (!objFileInfo.Exists) return;
        Console.WriteLine("got the change\r\n");
        global_functions.perform_action(Global_Vars.thereader.read(e.FullPath), Global_Vars.thepattern);
    }
}

Operating System is Win 7 Prof x64

  • 1
    what is the content of `filefilter` variable!Are you filtering for specific files..try `*.*` to enable raising events for any type of file – Anirudha Jul 28 '12 at 17:06
  • filefilter var is "*.eml" path var is c:\afolder – user1559904 Jul 28 '12 at 17:14
  • i had a typo in my xml-config file everything works fine. sorry for opening that thread – user1559904 Jul 28 '12 at 17:32
  • We'll need a complete but simple example that reproduces the problem. If I take your code and instantiate a Filewatcher object with `new Filewatcher(".", "*.*")` and call `start_watcher` on that instance then create file in the same directory as the exe, `OnChanged` is called – Peter Ritchie Jul 28 '12 at 18:10

1 Answers1

3

You don't make a call to start_watching(). Add a call to that and it may work better.

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98