简体   繁体   English

c++ 如何路径 LocalAppData 文件夹

[英]c++ How to path LocalAppData folder

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?如果有人知道,他们能解释一下我应该如何获得 LocalAppData 路径吗?

The correct way to do this is to call SHGetKnownFolderPath with FOLDERID_LocalAppData :正确的方法是使用FOLDERID_LocalAppData SHGetKnownFolderPath

#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.您还可以使用一个环境变量LOCALAPPDATA ,但不能保证它包含预期值,因为它可以由父进程修改。

you can use the SHGetFolderPath function (for maximum compatibility)您可以使用SHGetFolderPath function (以获得最大兼容性)

you have to specify the CSIDL value for the folder you want, in your case would be CSIDL_LOCAL_APPDATA你必须为你想要的文件夹指定CSIDL值,在你的情况下是CSIDL_LOCAL_APPDATA

after windows vista you can instead use SHGetKnowFolderPath , which also requires the folder KNOWFOLDERID , in your case this would be FOLDERID_LocalAppData .在 windows vista 之后,您可以改用SHGetKnowFolderPath ,这也需要文件夹KNOWFOLDERID ,在您的情况下,这将是FOLDERID_LocalAppData For use this you have to include shlobj.h要使用它,您必须包含shlobj.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM