无法在 C# 中使用 FileSystemWatcher 监视文件夹

发布于 2024-11-08 19:31:49 字数 599 浏览 1 评论 0原文

我正在尝试监视文件夹(及其子文件夹)的更改。但是,我的处理程序事件从未执行。我正在使用以下代码:

FileSystemWatcher m_Watcher = new FileSystemWatcher();
m_Watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/Portal 2 Map Installer/"; 
m_Watcher.Filter = "";
m_Watcher.NotifyFilter = NotifyFilters.LastAccess |
             NotifyFilters.LastWrite |
             NotifyFilters.FileName |
             NotifyFilters.DirectoryName;
m_Watcher.IncludeSubdirectories = true;
m_Watcher.Changed += new FileSystemEventHandler(OnFolderChange);
m_Watcher.EnableRaisingEvents = true;

请帮忙!

I am attempting to monitor a folder (and its sub-folders) for changes. However, my handler event is never executed. I am using the following code:

FileSystemWatcher m_Watcher = new FileSystemWatcher();
m_Watcher.Path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/Portal 2 Map Installer/"; 
m_Watcher.Filter = "";
m_Watcher.NotifyFilter = NotifyFilters.LastAccess |
             NotifyFilters.LastWrite |
             NotifyFilters.FileName |
             NotifyFilters.DirectoryName;
m_Watcher.IncludeSubdirectories = true;
m_Watcher.Changed += new FileSystemEventHandler(OnFolderChange);
m_Watcher.EnableRaisingEvents = true;

Help please!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

五里雾 2024-11-15 19:31:49

为错误事件创建一个处理程序并查看它的内容:

  m_Watcher.Error += new ErrorEventHandler(OnError);

Create a handler for the on error event and see what it says:

  m_Watcher.Error += new ErrorEventHandler(OnError);
此刻的回忆 2024-11-15 19:31:49

有一些关于已更改事件处理程序:

重命名文件时会意外引发 Changed 事件,但重命名目录时不会引发该事件。要监视重命名,请使用 Renamed 事件.

因此,谨慎处理 Renamed< /code>事件也是如此(至少)。

There are some notes on the Changed event handler :

The Changed event is raised unexpectedly when a file is renamed, but is not raised when a directory is renamed. To watch for renaming, use the Renamed event.

So it would be prudent to handle the Renamed event as well (at the least).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文