2017-04-17 24 views
1

別のスレッドにFileSystemWatcherを作成して、ディレクトリの変更を監視します。新しいファイルを追加したり、監視しようとしているディレクトリに新しいファイルをコピーしたりすると、私のイベントは発生しません。私はWindows FormsアプリケーションでFileSystemWatcherクラスを正常に使用していますので、私は単純なものがないと思います。FileSystemウォッチャーがイベントを発生させないのはなぜですか?

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

あなたはそれ以外の場合は、いずれかのイベントが発生しません、(それがデフォルトでfalseだ)そのEnableRaisingEvents propertytrueに設定する必要があります。

watcher.EnableRaisingEvents = true; 
+0

ありがとうございます。私の問題を解決しました。 :) –

+0

@BillGreer:うれしい!がんばろう! ;) –

関連する問題