如何在Windows中使用C ++获取应用程序数据path?

我看了整个互联网,似乎没有一个体面的解决scheme,我可以find。 我希望能够以编程方式在C ++中获取该path“%ALLUSERSPROFILE%\ Application Data”,该资源pipe理器可以将其转化为一个真正的path。

我可以做到这一点,而不依赖于第三方代码?

使用SHGetFolderPathCSIDL_COMMON_APPDATA作为CSIDL。

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

只是为了interjay的答案

  1. 我不得不包含shlobj.h使用SHGetFolderPath

  2. 通常你可能需要从appdata中读取一个文件,为此你需要使用pathAppend函数( shlwapi.h是需要的)。

 #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\\") ); } 

在这里看到更多的细节。

您也可以从注册表中读取值

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

key = Common AppData