How to get user home directory on Windows?
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
ruipacheco
Updated on June 11, 2022Comments
-
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 about 7 yearsAttenzion!! Only can use for desktop apps in ver 5.2 ~ 5.3. - MSDN [msdn.microsoft.com/en-us/library/windows/desktop/…
-
ruipacheco about 7 yearsThat page is a 404.
-
ruipacheco about 7 yearsCould those vars be overwritten in a desktop/phone app?
-
Harry Johnston about 7 yearsNo, no, no! The profile directory is NOT the right place for configuration files. Try
CSIDL_APPDATA
. -
Harry Johnston about 7 yearsThe user profile is not the right location for configuration files.
-
Harry Johnston about 7 yearsThat variable doesn't always exist.
-
bweber about 7 yearsFrom what I understood he wanted the user's home directory.
-
Bryant about 7 yearsYou have to remove last character "]"! I think you're not attenzion on my answer. I won;t type anymore.
-
Harry Johnston about 7 yearsThere'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 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 about 7 years@ruipacheco Doubtful that they'd be overwritten or changed.
-
Bryant almost 7 yearsThank you for all answers! ruipacheco.
-
Adrian Maire over 4 yearsSHGetFolderPathW seems deprecated
-
Gregor Watters Härtl about 2 yearsThis is brilliant! Thank you! I've always been wary of relying on environment variables, as they can be changed...