Filesystemwatcher不会触发事件

问题描述:

我正在尝试使用FileSystemWatcher类监视目录中的新文件.

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);
    }
}

操作系统是Win 7 Prof x64

Operating System is Win 7 Prof x64

您不会调用start_watching().添加一个呼叫,可能会更好.

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