简体   繁体   English

使用 C++ 获取 %APPDATA% 路径

[英]get %APPDATA% path using c++

I want to get the path to the %APPDATA% folder.我想获取%APPDATA%文件夹的路径。

In win 2000 & xp it's in: C:\Documents and Settings\user name\Application Data在win 2000 & xp中是在: C:\Documents and Settings\user name\Application Data

In vista & win7 it's in: C:\Users\user name\AppData\Roaming在vista & win7中是在: C:\Users\user name\AppData\Roaming

I know there is the function SHGetSpecialFolderPath but it retrieves a BOOL and I want to get the path as a string.我知道有函数SHGetSpecialFolderPath但它检索一个 BOOL 并且我想获取字符串形式的路径。

The third parameter of SHGetSpecialFolderPath() , named lpszPath , is marked as __out . SHGetSpecialFolderPath()的第三个参数,名为lpszPath ,标记为__out

Something like this should do:像这样的事情应该做:

// Beware, brain-compiled code ahead!
wchar_t buffer[MAX_PATH];
BOOL result = SHGetSpecialFolderPath( hWnd
                                    , buffer
                                    , CSIDL_LOCAL_APPDATA
                                    , false );
if(!result) throw "You'll need error handling here!";
std::wcout << buffer;

Note: I haven't done any Win API work in years.注意:我已经很多年没有做过任何 Win API 工作了。 Very likely someone comes along shortly pointing out where I blew it.很可能有人会很快指出我在哪里搞砸了。

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

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