2012-07-28 177 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