How to get user home directory on Windows?

19,571

For a console application the simplest method is to either retrieve the USERPROFILE environment variable or concatenate the values of the HOMEDRIVE and HOMEPATH environment variables.

Use the getenv() function in the standard library: https://msdn.microsoft.com/en-us/library/tehxacec.aspx

Example program:

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char** argv) {
    printf("USERPROFILE = %s\n", getenv("USERPROFILE"));
    printf("HOMEDRIVE   = %s\n", getenv("HOMEDRIVE"));
    printf("HOMEPATH    = %s\n", getenv("HOMEPATH"));
    return 0;
}

Output:

USERPROFILE = C:\Users\myuser
HOMEDRIVE   = C:
HOMEPATH    = \Users\myuser
Share:
19,571
ruipacheco
Author by

ruipacheco

Updated on June 11, 2022

Comments

  • ruipacheco
    ruipacheco almost 2 years

    I'm developing a cross-platform library that is meant to load configuration files from a user's home directory. The idea is to automatically provide configuration parameters without editing code.

    This library can be used in desktop apps or in daemons/services. In (I assume) most Unix environments I can use getpwuid() to get the home directory of the user. In Windows SO told me I could use SHGetKnownFolderPath but its documentation says its for desktop apps only. Is there a way to get this path, on Windows, for the user running a service?

  • Bryant
    Bryant about 7 years
    Attenzion!! Only can use for desktop apps in ver 5.2 ~ 5.3. - MSDN [msdn.microsoft.com/en-us/library/windows/desktop/…
  • ruipacheco
    ruipacheco about 7 years
    That page is a 404.
  • ruipacheco
    ruipacheco about 7 years
    Could those vars be overwritten in a desktop/phone app?
  • Harry Johnston
    Harry Johnston about 7 years
    No, no, no! The profile directory is NOT the right place for configuration files. Try CSIDL_APPDATA.
  • Harry Johnston
    Harry Johnston about 7 years
    The user profile is not the right location for configuration files.
  • Harry Johnston
    Harry Johnston about 7 years
    That variable doesn't always exist.
  • bweber
    bweber about 7 years
    From what I understood he wanted the user's home directory.
  • Bryant
    Bryant about 7 years
    You have to remove last character "]"! I think you're not attenzion on my answer. I won;t type anymore.
  • Harry Johnston
    Harry Johnston about 7 years
    There's always been some disagreement about how best to respond when someone asks the wrong question. I'm firmly in the camp that says you give them the information they need, rather than (or as well as) the information they asked for.
  • Harry Johnston
    Harry Johnston about 7 years
    ... besides, the user's home directory isn't necessarily their profile directory anyway. That's just the default if the user account settings don't specify a home directory explicitly. (I'm just nitpicking here, the distinction doesn't really matter, particularly since no modern application should use either the profile directory or the home directory for anything.)
  • Paul
    Paul about 7 years
    @ruipacheco Doubtful that they'd be overwritten or changed.
  • Bryant
    Bryant almost 7 years
    Thank you for all answers! ruipacheco.
  • Adrian Maire
    Adrian Maire over 4 years
    SHGetFolderPathW seems deprecated
  • Gregor Watters Härtl
    Gregor Watters Härtl about 2 years
    This is brilliant! Thank you! I've always been wary of relying on environment variables, as they can be changed...