How do I get the application data path in Windows using C++?

一个人想着一个人 提交于 2019-11-26 13:57:24

问题


I looked all over the internet and there doesn't seem to be a decent solution that I could find. I want to be able to programmatically in C++ obtain the path "%ALLUSERSPROFILE%\Application Data" that explorer can translate into a real path.

Can I do this without relying on third-party code?


回答1:


Use SHGetFolderPath with CSIDL_COMMON_APPDATA as the CSIDL.

TCHAR szPath[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, szPath)))
{
    //....
}



回答2:


Just to suppliment interjay's answer

  1. I had to include shlobj.h to use SHGetFolderPath.

  2. Often you may need to read a file from appdata, to do this you need to use the pathAppend function (shlwapi.h is needed for this).

#include <shlwapi.h>
#pragma comment(lib,"shlwapi.lib")
#include "shlobj.h"

TCHAR szPath[MAX_PATH];
// Get path for each computer, non-user specific and non-roaming data.
if ( SUCCEEDED( SHGetFolderPath( NULL, CSIDL_COMMON_APPDATA, NULL, 0, szPath ) ) )
{
    // Append product-specific path
    PathAppend( szPath, _T("\\My Company\\My Product\\1.0\\") );
}

See here for more details.




回答3:


you can also read the value from the registry

path = HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

key = Common AppData



来源:https://stackoverflow.com/questions/2899013/how-do-i-get-the-application-data-path-in-windows-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!