Filesystemwatcher不会触发事件 [英] Filesystemwatcher doesn't trigger event

查看:218
本文介绍了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.

这篇关于Filesystemwatcher不会触发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆