【问题标题】:Why is my FileSystem Watcher not firing events?为什么我的 FileSystem Watcher 没有触发事件?
【发布时间】:2017-04-17 10:55:12
【问题描述】:

我在单独的线程上创建了一个 FileSystemWatcher 来监视目录的更改。当我添加新文件或将新文件复制到我试图监控的目录中时,我的任何事件都不会触发。我已经在 Windows 窗体应用程序中成功使用了 FileSystemWatcher 类,所以我猜我遗漏了一些简单的东西。

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();
    }
}

【问题讨论】:

标签: c# wpf filesystemwatcher


【解决方案1】:

您需要将其EnableRaisingEvents property 设置为true(默认为false),否则不会引发任何事件。

watcher.EnableRaisingEvents = true;

【讨论】:

  • 谢谢。解决了我的问题。 :)
  • @BillGreer:很高兴我能帮上忙!祝你好运! ;)
猜你喜欢
  • 1970-01-01
  • 2021-01-18
  • 2012-01-11
  • 2011-05-05
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多