如何通过c程序在linux中找到默认下载目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13161226/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:40:18  来源:igfitidea点击:

How to find default download directory in linux through c program?

linuxdownloadubuntu-12.04

提问by Whoami

As I titled,

正如我所说,

is there any API available, to determine the default download directory in the Linux?

是否有任何可用的 API 来确定 Linux 中的默认下载目录?

I am using ubuntu distribution.

我正在使用 ubuntu 发行版。

回答by logoff

I think the best approximation is using HOME environment variable of the user. In many distros you can use then subfolders inside, but it does not apply to all distros.

我认为最好的近似方法是使用用户的 HOME 环境变量。在许多发行版中,您可以在里面使用 then 子文件夹,但它并不适用于所有发行版。

回答by Andy Ross

There is no "default download directory" at the level of the OS, though all current distros will create a ~/Downloadsdirectory for this purpose and most browsers will choose it by default. What are you trying to accomplish?

操作系统级别没有“默认下载目录”,尽管当前所有发行版都会~/Downloads为此目的创建一个目录,并且大多数浏览器会默认选择它。你想达到什么目的?

回答by alk

There are no standards for how to organise user data on Linux, besides that in most of the cases it goes to where $HOMEpoints to.

在 Linux 上如何组织用户数据没有标准,除了大多数情况下它指向的地方$HOME

One excpetion to this is where to place and how to name files and directories to store user specific application configruations. They shall go to the root of the users home, and those file/directories shall start with a .. Please see here for this.

对此的一个例外是放置以及如何命名文件和目录以存储用户特定的应用程序配置。它们应转到用户主目录的根目录,这些文件/目录应以 .. 请在此处查看

For system files there do exist guide lines, namely the FHS.

对于系统文件,确实存在指南,即FHS

回答by djasa

Quite late to the party but: There is XDG specification that covers exactly this topic. To get the download directory in shell (script) just run:

聚会很晚但是:XDG 规范正好涵盖了这个主题。要在 shell(脚本)中获取下载目录,只需运行:

$ xdg-user-dir DOWNLOAD
/home/djasa/Downloads

For programs, the location is available in $HOME/.config/user-dirs.dirs:

对于程序,该位置位于$HOME/.config/user-dirs.dirs

$ grep DOWNLOAD .config/user-dirs.dirs 
XDG_DOWNLOAD_DIR="$HOME/Downloads"

For more information about XDG, read man pages that man -k xdgreturns to you.

有关 XDG 的更多信息,请阅读man -k xdg返回给您的手册页。