I want to access the user name in the Windows using C programming and use that name to create the path to the particular file like "c:\users\john\Roaming.....and so on". So for every system user name e.g "john" is different. Help me to find the user name at run time.
-
Do you need to get only the current user's name or [all user names in the system](http://stackoverflow.com/questions/1376036/getting-a-list-of-user-profiles-on-a-computer-in-c-win32)? – Eugene Podskal Jul 01 '14 at 06:43
-
`getenv("USERNAME")` should do it. – Jean-François Fabre Sep 27 '17 at 14:59
-
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724432(v=vs.85).aspx – Rivasa Sep 27 '17 at 14:59
6 Answers
#include <stdio.h>
int main(void)
{
printf("%s\n", getenv("USERPROFILE")); // Print user's home directory.
return 0;
}
To get the user name instead of the home path replace USERPROFILE
with USERNAME
.

- 25,316
- 23
- 100
- 147
What you are looking for, here, is probably more SHGetKnownFolderPath
. The function lets you find per-user special folders. This is preferred to querying usernames because the home folder may not have the same name as the user.
WSTR* location;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &location);
if (SUCCEEDED(hr))
{
// location contains the folder path
// call CoTaskMemFree to free up the memory once you're done with it
CoTaskMemFree(location);
}
The list of so-called known folders is available here.

- 134,922
- 42
- 253
- 328
-
This is most likely what the OP needs. To get the name of each individual user doesn't make much sense – Lundin Jul 01 '14 at 06:30
The function to get user name on windows is GetUserName
This answer, probably, will help you too.
-
2All basic Win32 APIs are C and so is `GetUserName` why do you say it's `C++`? Did you try clicking on the link in the answer, it's not the one in your comment to the question. – legends2k Jul 01 '14 at 06:24
-
The tab-like heading of the box containing the function declaration does say C++. I guess MSDN simply conflates C and C++. – user4815162342 Jul 01 '14 at 06:39
you could use the following code to get the Username.
#include <stdlib.h>
void main(void)
{
//following gets the appdata folder
char szAppData[1024];
char * szBufer = 0;
szBufer = getenv ("APPDATA");
if (szBufer != NULL)
{
strcpy(szBufer , szAppData);
}
//following code gets the user name
char szOSUserName[1024];
szBufer = getenv ("USERNAME");
if (szBufer != NULL)
{
strcpy(szBufer , szOSUserName);
}
}

- 80
- 5
You can get the name of the current user with GetUserName
:
#include <Windows.h>
#include <Lmcons.h>
#include <stdio.h>
int main()
{
char name[UNLEN + 1];
DWORD cch = UNLEN + 1;
if (GetUserName(name, &cch))
{
char cmd[100 + UNLEN + 1];
sprintf(cmd, "echo The username is \"%s\"", name); // Silly demo command
system(cmd);
}
return 0;
}
Use GetUserNameEx
if you want the name in a specific format.
If you need to get the path to a special folder like "My Documents" or "Desktop" you should use the special folder functions like SHGetFolderPath
or SHGetKnownFolderPath
.

- 97,548
- 12
- 110
- 164
%USERNAME%
will give you the username, but a better solution is to store it on %USERPROFILE%\\Desktop\\key.txt
to at least make it OS-independent.
And an even better solution would be not to store private information on the users' desktops. Or anywhere.

- 65,249
- 10
- 91
- 131
-
1I stored it on desktop just for testing, not going to really save it there :D – IP002 Sep 27 '17 at 15:02
-
1This question is tagged as C, only batch files should use environment variables. – Anders Sep 27 '17 at 17:57
-
The Desktop folder might not be located in the users profile root! I could also do "set USERNAME=Administrator" before starting the program... – Anders Sep 27 '17 at 18:07
-
1@Anders, you can delete your own hard drive if you want, how is that in any way relevant to anything? – Blindy Sep 27 '17 at 18:21
-
Why would you rely on fragile environment variables when there are documented functions you can use instead that always gives the correct result? Your "%USERPROFILE%\Desktop" example will return the wrong path for some people, it should not be a part of any marked answer IMHO. Another example then. Some build tools like SCONS will delete most environment variables and reset things like %PATH% so it has a clean and repeatable environment. – Anders Sep 27 '17 at 18:29
-