1

I am in need to open the default file manager of the user's system (not a dialog, but the file manager used by the system) using gtk and C.

meaning, when a user chooses a directory, file manager will open and not a dialog. is there any way to achieve this?

2 Answers 2

8

You should use GIO:

GError *error = NULL; if (!g_app_info_launch_default_for_uri ("file:///", NULL, &error)) { g_warning ("Failed to open uri: %s", error->message); } 

Note that this almost certainly requires a running GLib main loop (but if you're using GTK+, you already have that).

Also, there's no 100% guarantee that a file manager will be opened, but on any sanely set up system that is going to be the default handler for uris that are directories.

On a normal GNOME system @keltars xdg-open method forks, runs a shell script that starts a binary that then runs the same g_app_info_launch_default_for_uri() function.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the useful and important add!
2

You should launch xdg-open, e.g. with ordinary fork & exec.

E.g. this will open default file manager at /:

if(fork() == 0) { system("xdg-open /"); } 

5 Comments

Wanted to clearify this, if I do system("xdg-open ~/Music"); It will open for me 2 directories. directory "/" and directory "Music". Is there any way to get past it, So I will open only "Music" folder?
Weird. Does it happen if you launch this command from terminal? Is that the only change in code you've made?
nope not from the command line, only when called from the code it will open the root directory also
It happens on debian also, opens root directory and directory specified
Cannot reproduce. Can you provide minimal verifiable example either by editing your question or by asking separate one?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.