代码之家  ›  专栏  ›  技术社区  ›  user1559904

文件系统观察程序未触发事件

  •  0
  • user1559904  · 技术社区  · 12 年前

    我正在尝试用监视目录中的新文件 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

    1 回复  |  直到 10 年前
        1
  •  3
  •   Peter Ritchie    12 年前

    您不会调用start_watching()。再加上一个电话,效果可能会更好。