8

How can I get the path to the current User's home directory?

Ex: In Windows, if the current user is "guest" I need "C:\Users\guest"

My application will run on most of the Windows versions (XP, Vista, Win 7).

Julien-L
  • 5,267
  • 3
  • 34
  • 51
Ullan
  • 1,311
  • 8
  • 21
  • 34
  • 1
    Are you looking for the current path, or home directory? Please put some effort into asking your question (and wording/spelling it correctly) if you'd like others to put effort into helping you. – Adam Liss Mar 03 '12 at 01:54
  • I apologize. I need to get the user's path. In windows 7 each logged user have a account under the folder "users". If my login account is HPFE, i need to get the path "C:\users\hpfe". – Ullan Mar 03 '12 at 01:57

4 Answers4

16

Use the function SHGetFolderPath. This function is preferred over querying environment variables since the latter can be modified to point to a wrong location. The documentation contains an example, which I repeat here (slightly adjusted):

#include <Shlobj.h>  // need to include definitions of constants

// .....

WCHAR path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path))) {
  ...
}
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
Philipp
  • 48,066
  • 12
  • 84
  • 109
4

I have used %USERPROFILE% to get path the current User's home directory.

Vasily Menshev
  • 369
  • 3
  • 4
4

Just use the environment variables, in this particular case you want %HOMEPATH% and combine that with %SystemDrive%

http://en.wikipedia.org/wiki/Environment_variable#Default_Values_on_Microsoft_Windows

scibuff
  • 13,377
  • 2
  • 27
  • 30
  • 1
    Is there any chance, System Drive can be "C:" and HomePath can be E:\Users\{username} – Ullan Mar 03 '12 at 02:03
  • possibly - http://lifehacker.com/5467758/move-the-users-directory-in-windows-7 - but that would be just a "simlink" so C:\Users would point to E:\Users so the path will be resolved anyways – scibuff Mar 03 '12 at 02:05
  • 1
    This might be the case for Windows 7, but I'm quite confident that at least under Windows XP you can actually move profile directories to another volume, so you should never use the `SystemDrive` environment variable. Please use `SHGetFolderPathW`, it is always correct, works on all supported Windows versions, and is easier to use. – Philipp Mar 03 '12 at 03:06
  • 6
    It is **`%HOMEDRIVE%`**, NOT `%SystemDrive%`, that you should be using with `%HomePath%`. – gregmac Nov 14 '14 at 15:53
-2

Approach 1:

#include <Shlobj.h>

std::string desktop_directory(bool path_w)
{
    if (path_w == true)
    {
        WCHAR path[MAX_PATH + 1];
        if (SHGetSpecialFolderPathW(HWND_DESKTOP, path, CSIDL_DESKTOPDIRECTORY, FALSE))
        {
            std::wstring ws(path);
            std::string str(ws.begin(), ws.end());
            return str;
        }
        else return NULL;
    }
}

Approach 2:

#include <Shlobj.h>

LPSTR desktop_directory()
{
    static char path[MAX_PATH + 1];
    if (SHGetSpecialFolderPathA(HWND_DESKTOP, path, CSIDL_DESKTOPDIRECTORY, FALSE)) return path;
    else return NULL;
}
  • Could you briefly explain the difference between the two please? – G_V Feb 07 '18 at 13:02
  • Your code returns the desktop directory, not the home directory as requested. Replace CSIDL_DESKTOPDIRECTORY with CSIDL_PROFILE (and rename the functions appropriately) to return the user's home directory as requested. – Michael Krebs Oct 04 '18 at 14:16
  • Additionally, approach 2 returns a pointer to static memory, which will break if this is used in a process shared by multiple users. If two calls to approach 2 return different values, the first return value will "magically" change, because the second call will reuse the same memory space for its return value as the first call. Do not use approach 2 in production without careful consideration. – Michael Krebs Oct 04 '18 at 14:16
  • 1
    Furthermore, approach 1 does not return a value on all code paths (such as if path_w is false) – Michael Krebs Oct 04 '18 at 14:21
  • Also, SHGetSpecialFolderPathW is no longer supported, currently replaced by SHGetKnownFolderPath. – Michael Krebs Oct 04 '18 at 14:27
  • returning NULL instead of std::string is probably a very bad idea. You could use `nullptr` instead of `NULL`. – Adrian Maire Sep 03 '19 at 12:13