How to get the current user's home directory in Windows

前端 未结 4 1825
野的像风
野的像风 2020-12-18 00:29

How can I get the path to the current User\'s home directory?

Ex: In Windows, if the current user is \"guest\" I need \"C:\\Users\\guest\"

My application wil

4条回答
  •  春和景丽
    2020-12-18 01:16

    Use the function SHGetFolderPath. This function is preferred over querying environment variables since the latter can be modified to point to a wrong location. The documentation contains an example, which I repeat here (slightly adjusted):

    #include   // need to include definitions of constants
    
    // .....
    
    WCHAR path[MAX_PATH];
    if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path))) {
      ...
    }
    

提交回复
热议问题