GIO tutorial: Launching files

In this short tutorial we’ll look at several ways we can use GIO to launch (or “open”) files.

Note: This article is part of my ongoing GIO tutorial series, which currently consists of: (1) File and path operations; (2) File IO; and (3) Launching files (this document).

Quick recap: GIO is GLib’s IO library. File and path operations are mostly done through the GFile class, while IO operations are done through the GInputStream and GOutputStream interfaces.

This tutorial shows, in essence, GIO’s cross-platform version of os.startfile or xdg-open.

Note that for this tutorial I’m using Vala instead of Python, partly because of the rather unfortunate state of PyGObject in Windows at the time of writing, and partly because I can. (Note: the Vala documentation has no future-proof URI; by now these links have gone to rot, so just use the C documentation.)

Nice way (buggy)

The simplest way to launch a file is with GAppInfo’s launch_default_for_uri static/class method (C reference, Vala reference), which is currently not available in PyGObject. To make matters worse, it doesn’t seem to work in Windows because “URIs not supported”; I have no idea what’s up with that.

// Currently broken.
var result = AppInfo.launch_default_for_uri ("file:///C:/Windows/explorer.exe", null);

Short but strange way (buggy)

Oddly enough, GTK+ (not GLib) has a show_uri method (C reference, Vala reference) polluting its root namespace. In Windows, calling this method results in the same “URIs not supported” error as the previous way.

// Currently broken.
var result = Gtk.show_uri (null,

“Please just do it” way

The safest way to launch a file is to obtain the default handler of the file, and then launch it with the file as argument. We do this by calling GFile’s query_default_handler (C reference, Vala reference), which returns a GAppInfo referring to the default handler. We then call its launch method, passing the GFile wrapped in a GList.

var file = File.new_for_path ("C:\\Windows\\explorer.exe");
var handler = file.query_default_handler (null);
var list = new List<File> ();
list.append (file);
var result = handler.launch (list, null);

Pointless way

The last way is to find out the file’s content type, get the default handler for that content type, and launch it. There’s not much point in using this one, I’m just including it as another alternative.

var file = File.new_for_path ("C:\\Windows\\explorer.exe");
var info = file.query_info ("standard::content-type",
var content_type = info.get_content_type ();
var handler = AppInfo.get_default_for_type (content_type, false);
var list = new List<File> ();
list.append (file);
var result = handler.launch (list, null);


There’s another GIO bug in Windows: it doesn’t support launching directories. I’ve filed GNOME bug 606337 accordingly.

1 thought on “GIO tutorial: Launching files

Note: By commenting, you grant me permission to freely republish your comment.

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.