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

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.


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.

Tags:

C++

Appdata