Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 596
dikidera Avatar asked May 07 '11 11:05

dikidera


People also ask

Is AppData on the C drive?

AppData is a hidden folder located in C:\Users\<username>\AppData. The AppData folder contains custom settings and other information needed by applications. For example, you might find the following in your AppData folder: Web browser bookmarks and cache.

How do I get to AppData in command prompt?

Run a command shell (start/Run, then "cmd") and type "set". This will list all the environmental variables available. Having said that, USERPROFILE is perfectly valid. There's also APPDATA and LOCALAPPDATA.


2 Answers

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.

like image 176
Cody Gray Avatar answered Sep 28 '22 16:09

Cody Gray


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.

like image 28
iptq Avatar answered Sep 28 '22 17:09

iptq