c++ - 如何在 Windows 中获取当前用户的主目录

标签 c++ windows

如何获取当前用户主目录的路径?

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

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

最佳答案

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

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

// .....

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

关于c++ - 如何在 Windows 中获取当前用户的主目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9542611/

相关文章:

c++ - 在源文件中定义方法时出错

c++ - <file> 所需的名称无效 - visual studio 2015

c++ - 在 C++ 中构建 libcurl 库,Noob 问题

c++ - VSCode+LLDB 调试在 Vagrant-Ubuntu 中无法正常工作

c++ - 包含在 header 或 cpp 中——Qt 中首选的是什么

windows - 内存映射文件 : how to monitor memory usage

c++ - 如何在 Driver.c controlFunction 接收使用 DeviceIoControl 发送的数据? IOCTL,驱动程序

windows - Cocoa 是 Windows 中 Win32 API 或 MFC 的 Mac 等价物吗?

c - shellexec 和 c :\program files - x86 and x64 portability

python - 在 Windows 上安装 virtualenvwrapper