JChan
JChan

Reputation: 1451

How to get the parent folder of a Windows user's profile path using C++

I am trying get the parent folder of a Windows user's profile path. But I couldn't find any "parameter" to get this using SHGetSpecialFolderPath, so far I am using CSIDL_PROFILE.

Expected Path:

Win7 - "C:\Users"

Windows XP - "C:\Documents and Settings"

Upvotes: 2

Views: 1292

Answers (2)

0xC0000022L
0xC0000022L

Reputation: 21269

With the shell libary version 6.0 you have the CSIDL_PROFILES (not to be confused with CSIDL_PROFILE) which gives you what you want. This value was removed (see here), you have to use your own workaround.

On any prior version you'll have to implement your own workaround, such as looking for the possible path separator(s), i.e. \ and / on Windows, and terminate the string at the last one. A simple version of this could use strrchr (or wcsrchr) to locate the backslash and then, assuming the string is writable, terminate the string at that location.

Example:

char* path;
// Retrieve the path at this point, e.g. "C:\\Users\\username"
char* lastSlash = strrchr(path, '\\');
if(!lastSlash)
  lastSlash = strrchr(path, '/');
if(lastSlash)
  *lastSlash = 0;

Or of course GetProfilesDirectory (that eluded me) which you pointed out in a comment to this answer.

Upvotes: 1

Dennis Gurzhii
Dennis Gurzhii

Reputation: 31

For most purposes other than displaying the path to a user, it should work to append "\\.." (or "..\\" if it ends with a backslash) to the path in question.

Upvotes: 2

Related Questions