windows 写入用户文档文件夹 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1268682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:59:11  来源:igfitidea点击:

Writing to user documents folder C++

c++windowswinapi

提问by user131091

I'm trying to write some info to the user's documents folder (eg. C:\Documents and Settings\[userName]), but I can't seem to find out how to grab the path programmatically. Is there any way to do this? C++, not using .NET.

我正在尝试将一些信息写入用户的文档文件夹(例如 C:\Documents and Settings\[userName]),但我似乎无法找到如何以编程方式获取路径。有没有办法做到这一点?C++,不使用 .NET。

Thanks!

谢谢!

回答by Michael

SHGetFolderPathwith CSIDL_PERSONAL can be used to get the user's Documents folder.

带有 CSIDL_PERSONAL 的SHGetFolderPath可用于获取用户的 Documents 文件夹。

WCHAR path[MAX_PATH];
HRESULT hr = SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL,
                             SHGFP_TYPE_CURRENT, path);

回答by anno

You could do this :

你可以这样做:

wchar_t *pUSERPROFILE;
size_t len;
_wdupenv_s( &pUSERPROFILE, &len, L"USERPROFILE" );
wstring userprofile = pUSERPROFILE;
free (pUSERPROFILE);    

_wdupenv_s MSDN

_wdupenv_s MSDN