How do I get the user name of the current user?

后端 未结 6 1489
醉梦人生
醉梦人生 2021-01-14 20:05

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\"

相关标签:
6条回答
  • 2021-01-14 20:43
    #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.

    0 讨论(0)
  • 2021-01-14 20:54

    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);
            }
        }
    
    0 讨论(0)
  • 2021-01-14 20:54

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

    0 讨论(0)
  • 2021-01-14 20:55

    The function to get user name on windows is GetUserName

    This answer, probably, will help you too.

    0 讨论(0)
  • 2021-01-14 20:59

    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.

    0 讨论(0)
  • 2021-01-14 21:03

    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.

    0 讨论(0)
提交回复
热议问题