有人在 C++/WinAPI 中有类似 FileSystemWatcher 的类吗?
我需要原始 C++/WinAPI 中的 .Net 的 FileSystemWatcher 模拟。 我几乎开始使用 FindFirstChangeNotification/FindNextChangeNotification 自己编写一个代码,但后来我想到我可能不是第一个需要这个的人,也许有人愿意分享。
理想情况下,我需要的是一个可以按如下方式使用的类:
FileWatcher fw;
fw.startWatching("C:\MYDIR", "filename.dat",
FileWatcher::SIZE | FileWatcher::LAST_WRITE,
&myChangeHandler);
...
fw.stopWatching();
或者如果它使用像 boost::signal 这样的东西,那就更好了。 但请注意,除了标准库、boost 和原始 WinAPI 之外,不要有任何依赖项。 谢谢!
I need a .Net's FileSystemWatcher analog in raw C++/WinAPI.
I almost started to code one myself using FindFirstChangeNotification/FindNextChangeNotification, but then it occurred to me that I am probably not the first one who needs this and maybe someone will be willing to share.
Ideally what I need is a class which can be used as follows:
FileWatcher fw;
fw.startWatching("C:\MYDIR", "filename.dat",
FileWatcher::SIZE | FileWatcher::LAST_WRITE,
&myChangeHandler);
...
fw.stopWatching();
Or if it would use somehting like boost::signal it would be even better.
But please, no dependencies other than the Standard Library, boost and raw WinAPI.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ReadDirectoryChangesW 函数怎么样?
http://msdn.microsoft.com/en-us/ library/aa365465(VS.85).aspx
它将通知存储在缓冲区中,这样您就不会错过任何更改(除非缓冲区溢出)
What about the ReadDirectoryChangesW function?
http://msdn.microsoft.com/en-us/library/aa365465(VS.85).aspx
It stores notifications in a buffer so you don't miss any changes (unless the buffer overflows)
2021 答案:
下面列出的存储库的分叉版本正在积极维护:https://github.com/SpartanJ/ efsw
旧答案:
这是一个跨平台解决方案,但可以很好地包装 Win32 内容:
https://github.com/jameswynn/simplefilewatcher
2021 answer:
A forked version of the repo listed below that is actively maintained: https://github.com/SpartanJ/efsw
Old answer:
This is a cross-platform solution, but does the job wrapping the Win32 stuff nicely:
https://github.com/jameswynn/simplefilewatcher
此处有一些公共域代码。我当前的项目使用这个(继承自以前的开发人员)。它工作得很好,但我们确实由于不清楚的原因而错过了通知(并且可能不是由此代码引起的)。
请注意,此处的 Win32 API 有一些限制,这使得很难/不可能避免丢失通知。 API 的背景和所谓的解决方法是 这里
There is some public-domain code here. My current project uses this (inherited from previous developers). It works pretty well but we do miss notifications for reasons that are unclear (and possibly not caused by this code).
Note that the Win32 API here has some limitations which make it difficult/impossible to avoid missing notifications. Background and alleged work-round for the API are here
http://msdn .microsoft.com/en-us/library/system.io.filesystemwatcher.created%28v=vs.71%29.aspx 上面是通过C#实现的,我们总是可以编写一个COM Wrapper
http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.created%28v=vs.71%29.aspx the above does throgh C#, we can always write a COM Wrapper
这是 ReadDirectoryChangesW 的示例,用go编写,
可以到这里获取完整代码。
This is an example of ReadDirectoryChangesW, written in go
You can go here to get the complete code.