Home > front end >  c How to path LocalAppData folder
c How to path LocalAppData folder

Time:02-02

I need to find the path of this file:

("C:\\Users\\*username*\\AppData\\Local")

I tried everything but I couldn't because of the username

If anyone knows, can they explain how I should get the LocalAppData path?

CodePudding user response:

The correct way to do this is to call SHGetKnownFolderPath with FOLDERID_LocalAppData:

#include <Windows.h>
#include <iostream>
#include <shlobj_core.h>
#include <KnownFolders.h>

int main() {
    PWSTR path = NULL;
    HRESULT hres = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &path);
    if (SUCCEEDED(hres)) {
        std::wcout << std::wstring(path) << std::endl;
        CoTaskMemFree(path);
    }
    return 0;
}

There is also an environment variable LOCALAPPDATA that you could use, but it's not guaranteed to contain the expected value since it can be modified by the parent process.

CodePudding user response:

You can use SHGetFolderPath() (for maximum compatibility).

You have to specify the CSIDL value for the folder you want, in your case that would be CSIDL_LOCAL_APPDATA.

After Windows Vista, you can instead use SHGetKnowFolderPath(), which requires a folder KNOWFOLDERID, in your case this would be FOLDERID_LocalAppData.

To use this, you have to include <shlobj.h>

  •  Tags:  
  • Related