如何在Windows中获取当前用户的主目录?

时间:2021-07-07 23:25:03

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"

在Windows中,如果当前用户是“guest”,我需要“C:\Users\guest”

My application will run on most of the Windows versions (XP, Vista, Win 7).

我的应用程序将在大多数Windows版本上运行(XP, Vista, Win 7)。

4 个解决方案

#1


11  

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):

使用函数SHGetFolderPath。该函数优于查询环境变量,因为后者可以修改为指向错误的位置。文档中有一个示例,我在这里重复一下(稍微调整一下):

#include <Shlobj.h>  // need to include definitions of constants

// .....

WCHAR path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path))) {
  ...
}

#2


5  

Just use the environment variables, in this particular case you want %HOMEPATH% and combine that with %SystemDrive%

只需要使用环境变量,在这个特定的情况下,您需要%主页%,并将其与%SystemDrive%合并。

http://en.wikipedia.org/wiki/Environment_variable#Default_Values_on_Microsoft_Windows

http://en.wikipedia.org/wiki/Environment_variable Default_Values_on_Microsoft_Windows

#3


1  

I have used %USERPROFILE% to get path the current User's home directory.

我使用%USERPROFILE%来获取当前用户的主目录。

#4


0  

Approach 1:

方法1:

#include <Shlobj.h>

std::string desktop_directory(bool path_w)
{
    if (path_w == true)
    {
        WCHAR path[MAX_PATH + 1];
        if (SHGetSpecialFolderPathW(HWND_DESKTOP, path, CSIDL_DESKTOPDIRECTORY, FALSE))
        {
            std::wstring ws(path);
            std::string str(ws.begin(), ws.end());
            return str;
        }
        else return NULL;
    }
}

Approach 2:

方法2:

#include <Shlobj.h>

LPSTR desktop_directory()
{
    static char path[MAX_PATH + 1];
    if (SHGetSpecialFolderPathA(HWND_DESKTOP, path, CSIDL_DESKTOPDIRECTORY, FALSE)) return path;
    else return NULL;
}

#1


11  

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):

使用函数SHGetFolderPath。该函数优于查询环境变量,因为后者可以修改为指向错误的位置。文档中有一个示例,我在这里重复一下(稍微调整一下):

#include <Shlobj.h>  // need to include definitions of constants

// .....

WCHAR path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PROFILE, NULL, 0, path))) {
  ...
}

#2


5  

Just use the environment variables, in this particular case you want %HOMEPATH% and combine that with %SystemDrive%

只需要使用环境变量,在这个特定的情况下,您需要%主页%,并将其与%SystemDrive%合并。

http://en.wikipedia.org/wiki/Environment_variable#Default_Values_on_Microsoft_Windows

http://en.wikipedia.org/wiki/Environment_variable Default_Values_on_Microsoft_Windows

#3


1  

I have used %USERPROFILE% to get path the current User's home directory.

我使用%USERPROFILE%来获取当前用户的主目录。

#4


0  

Approach 1:

方法1:

#include <Shlobj.h>

std::string desktop_directory(bool path_w)
{
    if (path_w == true)
    {
        WCHAR path[MAX_PATH + 1];
        if (SHGetSpecialFolderPathW(HWND_DESKTOP, path, CSIDL_DESKTOPDIRECTORY, FALSE))
        {
            std::wstring ws(path);
            std::string str(ws.begin(), ws.end());
            return str;
        }
        else return NULL;
    }
}

Approach 2:

方法2:

#include <Shlobj.h>

LPSTR desktop_directory()
{
    static char path[MAX_PATH + 1];
    if (SHGetSpecialFolderPathA(HWND_DESKTOP, path, CSIDL_DESKTOPDIRECTORY, FALSE)) return path;
    else return NULL;
}