3

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
niks
  • 39
  • 1
  • 1
  • 3

6 Answers6

8
#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.

Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
6

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.

zneak
  • 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
2

The function to get user name on windows is GetUserName

This answer, probably, will help you too.

Community
  • 1
  • 1
Anton K
  • 4,658
  • 2
  • 47
  • 60
  • 2
    All 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
2

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);
        }
    }
2

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.

Anders
  • 97,548
  • 12
  • 110
  • 164
1

%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.

Blindy
  • 65,249
  • 10
  • 91
  • 131
  • 1
    I stored it on desktop just for testing, not going to really save it there :D – IP002 Sep 27 '17 at 15:02
  • 1
    This 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
  • Noted, don't use SCONS. – Blindy Sep 27 '17 at 19:17