Quincy
Quincy

Reputation: 1939

How to resolve location of %UserProfile% programmatically in C++?

I'd like to find the directory of the current user profile programmatically in C++.

Upvotes: 5

Views: 5111

Answers (3)

Rupesh Shingavi
Rupesh Shingavi

Reputation: 41

Simplest way on Windows & Linux:

char *szBuff;
szBuff=std::getenv("USERPROFILE");  //Returning value of %USERPROFILE%

Upvotes: 4

Tom Cabanski
Tom Cabanski

Reputation: 8018

To cover all user profile scenarios in Windows Vista and up there is SHGetKnownFolderPath. Here is the link to the docs page on it and related functions.

Upvotes: 2

Chris Becke
Chris Becke

Reputation: 36026

SHGetSpecialFolderLocation is the best way to get at most of the special paths on Windows. Passed CSIDL_PROFILE it should retrieve the folder you are interested in.

If you are actually interested in the contents of the %UserProfile% environment variable you could try ExpandEnvironmentStrings

Upvotes: 10

Related Questions