none
获取 用户目录的AppData 目录 RRS feed

  • 问题

  • 您好,我想获取用户的目录:C:\Users\xxxUser\AppData,下面是我的代码

    系统登陆的用户名:Leo,

    1.以普通方式双击运行程序,得到的用户目录:C:\Users\Leo\AppData

    2.以右键管理员运行程序,得到的用户目录:C:\Users\Adminstrator\AppData

    代码如下:

    TCHAR outPath[MAX_PATH] = {0};
    LPITEMIDLIST ppidl = NULL;
    if (S_OK == SHGetSpecialFolderLocation(NULL, CSIDL_LOCAL_APPDATA, &ppidl) ) 
    {
    SHGetPathFromIDList(ppidl, outPath);
    CoTaskMemFree(ppidl);
    }

    我希望无论通过右键管理运行程序还是普通方式双击运行程序获取的都是当前系统登陆的用户的目录,如:

    C:\Users\Leo\AppData

    2020年7月3日 6:11

答案

  • 你好

    以下代码可以帮助你获取当前的登录用户名

    #include <windows.h>
    
    #include <WtsApi32.h>
    #pragma comment(lib, "WtsApi32.lib")
    #include <atlstr.h>
    
    #include <string>
    using namespace std;
    
    // 获取当前登录用户
    bool GetSessionUserName(string& strUserName)
    {
    	DWORD dwSessionId = WTSGetActiveConsoleSessionId();
    
    	LPTSTR pBuffer = NULL;
    	DWORD dwBufferLen;
    	BOOL bRes = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionId, WTSUserName, &pBuffer, &dwBufferLen);
    	if (bRes == FALSE)
    	{
    		return false;
    	}
    
    	CString schar = CString(pBuffer);
    	USES_CONVERSION;
    	 strUserName = string(T2A(schar));
    
    	WTSFreeMemory(pBuffer);
    
    	return true;
    }
    
    
    int main()
    {
    	string strUser;
    	if (GetSessionUserName(strUser))
    	{
    		printf("当前登录用户:%s\n", strUser.c_str());
    	}
    	else
    	{
    		printf("获取当前登录用户失败\n");
    	}
    
    	system("pause");
    
    	return 0;
    
    }


    Best Regards

    Barrnet Zhou


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2020年7月10日 5:11

全部回复

  • 你好

    我推荐你使用GetEnvironmentVariable('USERPROFILE'),%USERPROFILE% 可以帮助你获取WINDOWS下当前用户路径,关于GetEnvironmentVariable,可以参考官方文档:https://docs.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getenvironmentvariablea?redirectedfrom=MSDN

    Best Regards,

    Barrnet Zhou


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    2020年7月6日 2:28
  • 您好,这个方法我已经尝试过了

    双击运行Leo,获取的目录:C:\Users\Leo\AppData

    Leo用户登陆,右键以管理员运行,输入管理员账号:Administrator和密码,得到的目录:C:\Users\Administrator\AppData

    以下是我的代码:

    const TCHAR* homeProfile = _T("USERPROFILE");
    TCHAR homePath[1024] = { 0 };
    unsigned int pathSize = GetEnvironmentVariable(homeProfile, homePath, 1024);
    AfxMessageBox(homePath);

    2020年7月6日 10:24
  • 你好

    我想请问一下,Leo程序以管理员运行,在输入管理员账户和密码后,是否存在这样一个功能,程序根据登录的账户自动切换相应的目录下,导致显示的目录是C:\Users\Administrator\AppData?

    Best Regards

    Barrnet Zhou


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年7月7日 3:22
  • 您好,用户Leo(这是一个标准用户)登陆系统后,运行程序A,

    1.右键以管理员运行程序A,输入管理员的账号和密码,程序获取的用户目录是:C:\Users\Adminstrator\AppData
    2.双击运行程序A(正常运行程序),获取的用户目录是:
    C:\Users\Leo\AppData

    我希望无论用户是标准用户还是管理员用户,双击运行程序A或以管理员运行程序A都是获取实际运行的 用户目录,
    C:\Users\当前登陆用户\AppData,上面的例子我希望两种情况都是获取C:\Users\Leo\AppData

    2020年7月7日 6:21
  • 你好

    十分抱歉,我查找了很多文档,没有找到直接的函数能够满足你的需要,我建议你通过一些string操作,将outPath中的Administrator改成当前的登录用户。

    Best Regards

    Barrnet Zhou


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年7月8日 5:42
  • 是的,正因为我也寻找了很多资料都无法正确定位到所以才在这里求助呢?

    那这种情况下如何正确获取当前的登陆用户呢?

    2020年7月9日 12:15
  • 你好

    以下代码可以帮助你获取当前的登录用户名

    #include <windows.h>
    
    #include <WtsApi32.h>
    #pragma comment(lib, "WtsApi32.lib")
    #include <atlstr.h>
    
    #include <string>
    using namespace std;
    
    // 获取当前登录用户
    bool GetSessionUserName(string& strUserName)
    {
    	DWORD dwSessionId = WTSGetActiveConsoleSessionId();
    
    	LPTSTR pBuffer = NULL;
    	DWORD dwBufferLen;
    	BOOL bRes = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, dwSessionId, WTSUserName, &pBuffer, &dwBufferLen);
    	if (bRes == FALSE)
    	{
    		return false;
    	}
    
    	CString schar = CString(pBuffer);
    	USES_CONVERSION;
    	 strUserName = string(T2A(schar));
    
    	WTSFreeMemory(pBuffer);
    
    	return true;
    }
    
    
    int main()
    {
    	string strUser;
    	if (GetSessionUserName(strUser))
    	{
    		printf("当前登录用户:%s\n", strUser.c_str());
    	}
    	else
    	{
    		printf("获取当前登录用户失败\n");
    	}
    
    	system("pause");
    
    	return 0;
    
    }


    Best Regards

    Barrnet Zhou


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2020年7月10日 5:11
  • 谢谢你,这个是可以用户当前登陆用户
    2020年7月15日 6:41