2012-07-28 205 views
0

我正在尝试使用FileSystemWatcher类监视目录中的新文件。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的64位教授

+1

'filefilter'变量的内容是什么!您是否在过滤特定文件..try'*。*'以便为任何类型的文件启用事件 – Anirudha 2012-07-28 17:06:43

+0

filefilter var是“* .eml” path var is c :\文件夹 – user1559904 2012-07-28 17:14:05

+0

我在我的xml-config文件中有一个错字,一切正常。对不起,打开该线程 – user1559904 2012-07-28 17:32:24

回答

3

你不打个电话给start_watching()。添加一个电话,它可能会更好。

+0

问题解决了,如何关闭线程? – user1559904 2012-07-28 18:18:48

+0

接受彼得所做的答案。这将“关闭”该线程。 – 2012-07-28 18:28:24