如何监控 vala 中的目录?
How can I asynchronously monitor some directories in vala? All I need is for a callback method to be called whenever a file in one of the directories is:
- created
- deleted
- modified
I found GLib.FileMonitor but I am unsure how to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
始终回退到原始文档: http://developer.gnome.org/gio/unstable/ GFileMonitor.html
您可以从 GLib.File 创建监视器,然后连接到已更改信号。
Always fallback to the original documentation: http://developer.gnome.org/gio/unstable/GFileMonitor.html
You create a monitor from a GLib.File, then connect to the changed signal.
要监视目录,您需要首先使用 GLib.File.new_* 静态方法之一从该目录创建 GLib.File。 new_for_path 可能就是您想要的。
然后,您需要使用 monitor_directory 方法。
然后,您可以连接到已更改 GLib.FIleMonitor 对象的信号。
编译时,您需要包含
--pkg gio-2.0
。示例:
To monitor a directory, you need to first create a GLib.File from that directory by using one of the GLib.File.new_* static methods. new_for_path is probably what you want.
You then need to create a GLib.FileMonitor for that directory using the monitor_directory method of the GLib.File object.
You can then connect to the changed signal of the GLib.FIleMonitor object.
When you compile, you will need to include
--pkg gio-2.0
.Example:
这是一个仅使用 inotify 的 C 示例。虽然它是独立的,但可以将其修改为作为空闲进程工作(而不是 while(1))并调用回调(而不是 printf)
Here is a C example using only inotify. Although it is standalone, it can be modified to work as an idle process (instead of as while(1)) and call a callback (instead of printf)