C# 为什么我的文件系统监视程序不触发事件?
我在一个单独的线程上创建一个FileSystemWatcher来监视目录的更改。当我添加新文件或将新文件复制到我试图监视的目录中时,不会触发任何事件。我已经在Windows窗体应用程序中成功地使用了FileSystemWatcher类,所以我猜我遗漏了一些简单的东西C# 为什么我的文件系统监视程序不触发事件?,c#,wpf,filesystemwatcher,C#,Wpf,Filesystemwatcher,我在一个单独的线程上创建一个FileSystemWatcher来监视目录的更改。当我添加新文件或将新文件复制到我试图监视的目录中时,不会触发任何事件。我已经在Windows窗体应用程序中成功地使用了FileSystemWatcher类,所以我猜我遗漏了一些简单的东西 public partial class MainWindow : Window { System.IO.FileSystemWatcher watcher; public MainWindow() {
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();
}
}
您需要将其设置为true
(默认为false
),否则它不会引发任何事件
watcher.EnableRaisingEvents = true;
可能重复的你好那里!我还没有收到你的任何反馈,你解决问题了吗?谢谢。解决了我的问题。:)@比格里尔:很高兴我能帮忙!祝你好运