Linux 如何在gnome中获取桌面的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4358115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to obtain the path of Desktop in gnome
提问by FlareCat
I am using gnome/Ubuntu-10.10 in Chinese language and the desktop path is not "~/Desktop" but "~/XXXX" where XXXX stands for the Chinese translation of "Desktop". So, how to write code to obtain the proper path of desktop regardless of the system language? I noticed Qt SDK properly added a shortcut on the desktop so I guess there is a way. Thanks a lot!
我使用的是中文的 gnome/Ubuntu-10.10,桌面路径不是“~/Desktop”而是“~/XXXX”,其中 XXXX 代表“Desktop”的中文翻译。那么,无论系统语言如何,如何编写代码来获取桌面的正确路径?我注意到 Qt SDK 在桌面上正确添加了一个快捷方式,所以我想有一种方法。非常感谢!
采纳答案by FFire
I have not tested yet, but try echo echo ${XDG_DESKTOP_DIR:-$HOME/Desktop}, this cmd maybe do the work.
我还没有测试过,但试试 echo echo ${XDG_DESKTOP_DIR:-$HOME/Desktop},这个 cmd 可能会起作用。
回答by ptomato
The locations of the user directories are described in the xdg-user-dirsspecification. They provide some code herethat you can copy, to look up the name of the desktop directory from within your code.
用户目录的位置在xdg-user-dirs规范中描述。他们在此处提供了一些您可以复制的代码,以便从您的代码中查找桌面目录的名称。
回答by Tobu
One more way to do it:
另一种方法:
dir=$(xdg-user-dir DESKTOP)
回答by syam
echo ${XDG_DESKTOP_DIR:-$HOME/Desktop}
works fine on a local linux drive and also when the /home/<user>is mapped to an nfs drive.
在本地 linux 驱动器上以及/home/<user>映射到 nfs 驱动器时都可以正常工作。
回答by Federico Mena-Quintero
If you are using Glib, you can do
如果你使用的是 Glib,你可以这样做
const char *desktop_dir = g_get_user_special_dir (G_USER_DIRECTORY_DESKTOP);
This properly pays attention to the XDG environment variables and all that.
这正确地注意了 XDG 环境变量和所有这些。
回答by Antonin Décimo
The answer is provided by xdg-user-dirs specs:
test -f ${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs && source ${XDG_CONFIG_HOME:-~/.config}/user-dirs.dirs
echo ${XDG_DESKTOP_DIR:-$HOME/Desktop}
echo ${XDG_DOWNLOAD_DIR:-$HOME}

