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

后端 未结 6 1530
醉梦人生
醉梦人生 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: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.

提交回复
热议问题