2017-04-17 82 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:很高兴我能帮忙!祝你好运! ;) –

相关问题