如何使用 C++ 在 %appdata% 中打开文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5920853/
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 open a folder in %appdata% with C++?
提问by dikidera
As you all know, the appdata folder is this
众所周知,appdata文件夹就是这个
C:\Users\*Username*\AppData\Roaming
on windows 7
在 Windows 7 上
Since my application will be deployed on all kinds of Windows OSes i need to be able to get the folder 100% percent of the time. The question is how do you do it in C++? Since i don't know the the exact Windows OS it could be XP,Vista or 7 and most importantly i don't know what the Usernameis.
由于我的应用程序将部署在各种 Windows 操作系统上,因此我需要能够 100% 的时间获取文件夹。问题是你如何在 C++ 中做到这一点?因为我不知道确切的 Windows 操作系统,它可能是 XP、Vista 或 7,最重要的是我不知道用户名是什么。
回答by Cody Gray
For maximum compatibility with all versions of Windows, you can use the SHGetFolderPath
function.
It requires that you specify the CSIDLvalue for the folder whose path you want to retrieve. For the application data folder, that would be CSIDL_APPDATA
.
对于所有版本的Windows最大的兼容性,可以使用的SHGetFolderPath
功能。
它要求您为要检索其路径的文件夹指定CSIDL值。对于应用程序数据文件夹,这将是CSIDL_APPDATA
.
On Windows Vista and later, you should use the SHGetKnownFolderPath
functioninstead, which requires that you specify the folder's KNOWNFOLDERID
value. Again, for the application data folder, the appropriate value is FOLDERID_RoamingAppData
.
在 Windows Vista 和更高版本上,您应该改用该SHGetKnownFolderPath
函数,这要求您指定文件夹的KNOWNFOLDERID
值。同样,对于应用程序数据文件夹,适当的值为FOLDERID_RoamingAppData
。
To use either of these functions from your C++ application, you'll need to include shlobj.h
.
要从您的 C++ 应用程序中使用这些函数中的任何一个,您需要包含shlobj.h
.
回答by iptq
You can try the following:
您可以尝试以下操作:
char* appdata = getenv("APPDATA");
This code reads the environment variable APPDATA (you can also see it when you type SET
in a command window). It is set by Windows when your system starts.
此代码读取环境变量 APPDATA(您也可以SET
在命令窗口中键入时看到它)。它是在系统启动时由 Windows 设置的。
It will return the path of the user's appdata
as an absolute path, including Usernameand taking into account whichever OS version they're using.
它将appdata
以绝对路径形式返回用户的路径,包括用户名并考虑他们使用的任何操作系统版本。
回答by Chris Mauer
Perhaps fellow Googlers might find it interesting to have a look at std::filesystem. For instance, let's assume the default temp directory location and AppData directory structure in Windows 10:
也许 Google 的同事们可能会觉得看一看 std::filesystem 很有趣。例如,让我们假设 Windows 10 中的默认临时目录位置和 AppData 目录结构:
#include <filesystem>
auto path = std::filesystem::temp_directory_path()
.parent_path()
.parent_path();
path /= "Roaming";
if (!std::filesystem::exists(path))
std::filesystem::create_directories(path);
In the case of OP, I am assuming this doesn't solve the problem. I do want to raise a word of caution against doing the above in a situation that requires a 100% robust implementation, as system configurations can easily change and break the above.
在 OP 的情况下,我假设这不能解决问题。在需要 100% 稳健实现的情况下,我确实想提醒大家不要这样做,因为系统配置很容易改变和破坏上述情况。
But perhaps new visitors to the question might find std::filesystem useful. Chances are, you're going to want to manipulate the items in the directory if you're looking for it, and for this, std::filesystem can be your friend.
但也许这个问题的新访问者可能会发现 std::filesystem 很有用。很有可能,如果您正在寻找目录中的项目,您将想要操作它,为此, std::filesystem 可以成为您的朋友。