2017-04-17 77 views
1

我在一個單獨的線程上創建一個FileSystemWatcher來監視目錄中的更改。當我添加一個新文件或將一個新文件複製到我正在監視的目錄中時,我的任何事件都不會觸發。我在Windows Forms應用程序中成功地使用了FileSystemWatcher類,所以我猜測我錯過了一些簡單的東西。爲什麼我的FileSystem Watcher不會觸發事件?

public partial class MainWindow : Window 
{ 

    System.IO.FileSystemWatcher watcher; 
    public MainWindow() 
    { 
     InitializeComponent(); 
     System.Threading.Thread t1 = new System.Threading.Thread(MonitorDir); 
     t1.IsBackground = true; 
     t1.Start(); 
    } 

    private void MonitorDir() 
    { 

     watcher = new System.IO.FileSystemWatcher("C:\\Temp","*.*"); 
     watcher.Created += Watcher_Created; 
     watcher.Disposed += Watcher_Disposed; 
     watcher.Error += Watcher_Error; 
     watcher.Changed += Watcher_Changed; 
     while (true) 
     { 

     } 
    } 

    private void Watcher_Changed(object sender, System.IO.FileSystemEventArgs e) 
    { 
     throw new NotImplementedException(); 
    } 

    private void Watcher_Error(object sender, System.IO.ErrorEventArgs e) 
    { 
     throw new NotImplementedException(); 
    } 

    private void Watcher_Disposed(object sender, EventArgs e) 
    { 
     throw new NotImplementedException(); 
    } 

    private void Watcher_Created(object sender, System.IO.FileSystemEventArgs e) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

的存在[FileSystemWatcher的沒有觸發事件](http://stackoverflow.com/questions/16278783/filesystemwatcher-not-firing-events) –

+0

你好可能的複製!我沒有收到任何反饋,您是否設法解決您的問題? –

回答

2

您需要設置其EnableRaisingEvents propertytrue(這是false默認情況下),否則它不會引發任何事件。

watcher.EnableRaisingEvents = true; 
+0

謝謝。解決了我的問題。 :) –

+0

@BillGreer:很高興我能幫忙!祝你好運! ;) –

相關問題