Search code examples
c++appdata

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


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.


Solution

  • 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.