user1559904
user1559904

Reputation: 15

Filesystemwatcher doesn't trigger event

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

Upvotes: 0

Views: 1747

Answers (1)

Peter Ritchie
Peter Ritchie

Reputation: 35881

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

Upvotes: 3

Related Questions