为什么我的 FileSystem Watcher 没有触发事件?
Why is my FileSystem Watcher not firing events?
我在单独的线程上创建了一个 FileSystemWatcher 来监视目录的更改。 None 我的事件在我添加新文件或将新文件复制到我试图监视的目录时触发。我已经在 Windows Forms 应用程序中成功使用了 FileSystemWatcher class,所以我猜我遗漏了一些简单的东西。
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();
}
}
您需要将其 EnableRaisingEvents
property 设置为 true
(默认为 false
),否则不会引发任何事件。
watcher.EnableRaisingEvents = true;
我在单独的线程上创建了一个 FileSystemWatcher 来监视目录的更改。 None 我的事件在我添加新文件或将新文件复制到我试图监视的目录时触发。我已经在 Windows Forms 应用程序中成功使用了 FileSystemWatcher class,所以我猜我遗漏了一些简单的东西。
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();
}
}
您需要将其 EnableRaisingEvents
property 设置为 true
(默认为 false
),否则不会引发任何事件。
watcher.EnableRaisingEvents = true;