How to open a folder in %appdata% with C++?

狂风中的少年 提交于 2019-11-27 02:17:35

问题


As you all know, the appdata folder is this

 C:\Users\*Username*\AppData\Roaming

on 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 Username is.


回答1:


For maximum compatibility with all versions of Windows, you can use the SHGetFolderPath function.
It requires that you specify the CSIDL value for the folder whose path you want to retrieve. For the application data folder, that would be CSIDL_APPDATA.

On Windows Vista and later, you should use the SHGetKnownFolderPath function instead, which requires that you specify the folder's KNOWNFOLDERID value. Again, for the application data folder, the appropriate value is FOLDERID_RoamingAppData.

To use either of these functions from your C++ application, you'll need to include shlobj.h.




回答2:


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.

It will return the path of the user's appdata as an absolute path, including Username and taking into account whichever OS version they're using.




回答3:


Use this Code to reads the environment variable "APPDATA"
Include stdio.h file in beginning

char *pValue;
size_t len;
errno_t err = _dupenv_s(&pValue, &len, "APPDATA");


来源:https://stackoverflow.com/questions/5920853/how-to-open-a-folder-in-appdata-with-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!