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

4
  • 1
    what is the content of filefilter variable!Are you filtering for specific files..try *.* to enable raising events for any type of file Commented Jul 28, 2012 at 17:06
  • filefilter var is "*.eml" path var is c:\afolder Commented Jul 28, 2012 at 17:14
  • i had a typo in my xml-config file everything works fine. sorry for opening that thread Commented Jul 28, 2012 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 Commented Jul 28, 2012 at 18:10

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Accept the answer that Peter made. That will "Close" the thread.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.