简体   繁体   English

如何使用C ++ 17 std :: filesystem获取用户的主目录?

[英]How can one get user's home directory using C++17 std::filesystem?

I need to obtain the current user's home directory and then append to it paths to other directories. 我需要获取当前用户的主目录,然后将其追加到其他目录的路径。 Since the app is cross-platform then the approach should take this into consideration. 由于该应用程序是跨平台的,因此该方法应考虑到这一点。 I have been looking for a solution without success. 我一直在寻找没有成功的解决方案。 Is there a way to get current user's home directory using C++17 std::filesystem? 有没有一种方法可以使用C ++ 17 std :: filesystem获取当前用户的主目录?

No. 没有。

The only special directories you are handed is a directory suitable for temporary files, and the current directory. 您所获得的唯一特殊目录是适合临时文件的目录以及当前目录。

You'll have to write a OS specific wrapper for getting the home directory. 您必须编写特定于操作系统的包装程序才能获取主目录。 I, personally, think that is a bad idea: rather, you should have "user app settings" directory function, "user document'" directory function, etc (as determined by your application needs). 我个人认为这是一个坏主意:相反,您应该具有“用户应用程序设置”目录功能,“用户文档'”目录功能等(取决于您的应用程序需求)。 Where those directories are compared to the home directory vary on a OS by OS basis. 将这些目录与主目录进行比较的位置因操作系统而异。

In my experience, also having an API for storing a "registry-like property set" is a good idea (a string-string map with serialization code on the values, for example). 以我的经验,也有一个用于存储“类似注册表的属性集”的API是个好主意(例如,在值上带有序列化代码的字符串映射)。 It can map to plists on macOS, registry on windows, and something else on linux. 它可以映射到macOS上的复制者,Windows上的注册表以及Linux上的其他文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用文件系统库获取“真实”文件大小? (C ++ 17) - How to get “true” file size with filesystem library? (C++17) 错误使用<filesystem> C++17 - Errors using <filesystem> C++17 是C ++ 17 std :: filesystem :: recursive_directory_iterator :: pop等于boost :: filesystem :: recursive_directory_iterator :: no_push - is C++17 std::filesystem::recursive_directory_iterator::pop equal to boost::filesystem::recursive_directory_iterator::no_push C++ CodeBlocks 升级到 C++17 不能使用 std::filesystem - C++ CodeBlocks upgrade to C++17 can't use std::filesystem C ++ 17 std :: filesystem :: path中的本机路径分隔符错误? - Native path separator bug in C++17 std::filesystem::path? 如何在 C++17 中检索 std::filesystem::file_time_type 的时钟类型 - How to retrieve the clock type of std::filesystem::file_time_type in C++17 "c++14 std::experimental::filesystem::v1 和 c++17 std::filesystem 之间的区别?" - Differences between c++14 std::experimental::filesystem::v1 and c++17 std::filesystem? 为什么 Xcode 11 beta 不能使用 C++17<filesystem> 头? - Why Xcode 11 beta can't use C++17's <filesystem> header? 如何使用 C++17 的文件系统检查路径是否引用 Windows 中的卷? - How to check if a path refers to a volume in Windows with C++17's filesystem? 使用链接错误<filesystem> C++17 中的成员 - Link errors using <filesystem> members in C++17
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM