public abstract FileMonitor monitor_file (FileMonitorFlags flags, Cancellable? cancellable = null) throws IOError

Obtains a file monitor for the given file.

If no file notification mechanism exists, then regular polling of the file is used.

If cancellable is not null, then the operation can be cancelled by triggering the cancellable object from another thread. If the operation was cancelled, the error g_io_error_cancelled will be returned.

If flags contains g_file_monitor_watch_hard_links then the monitor will also attempt to report changes made to the file via another filename (ie, a hard link). Without this flag, you can only rely on changes made through the filename contained in this to be reported. Using this flag may result in an increase in resource usage, and may not have any effect depending on the FileMonitor backend and/or filesystem type.

Example: File monitoring:

public static int main (string[] args) {
try {
File file = File.new_for_path ("test.txt");
FileMonitor monitor = file.monitor_file (FileMonitorFlags.NONE, null);
print ("Monitoring: %s\n", file.get_path ());

monitor.changed.connect ((src, dest, event) => {
if (dest != null) {
print ("%s: %s, %s\n", event.to_string (), src.get_path (), dest.get_path ());
} else {
print ("%s: %s\n", event.to_string (), src.get_path ());

new MainLoop ().run ();
} catch (Error err) {
print ("Error: %s\n", err.message);
return 0;

valac --pkg gio-2.0 GLib.File.monitor_file.vala



input File


a set of FileMonitorFlags


optional Cancellable object, null to ignore


a FileMonitor for the given this, or null on error. Free the returned object with unref.