繁体   English   中英

Filesystemwatcher不会触发事件

[英]Filesystemwatcher doesn't trigger event

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

我的问题是事件不会被触发。 我的观察者类是:

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

您不会调用start_watching()。 添加一个电话,它可能会更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM