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

前端 未结 3 1228
北恋
北恋 2020-12-01 10:54

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 \"%ALLUSERSPRO

相关标签:
3条回答
  • 2020-12-01 11:12

    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.

    0 讨论(0)
  • 2020-12-01 11:18

    Use SHGetFolderPath with CSIDL_COMMON_APPDATA as the CSIDL.

    TCHAR szPath[MAX_PATH];
    if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, szPath)))
    {
        //....
    }
    
    0 讨论(0)
  • 2020-12-01 11:23

    you can also read the value from the registry

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

    key = Common AppData

    0 讨论(0)
提交回复
热议问题