如何获取当前用户的主目录在Windows [英] How to get the current user's home directory in Windows

查看:1509
本文介绍了如何获取当前用户的主目录在Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

推荐答案

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

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 <Shlobj.h>  // need to include definitions of constants

// .....

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

这篇关于如何获取当前用户的主目录在Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆