https://stackoverflow.com/questions/2414828/get-path-to-my-documents
#include <windows.h>
#include <iostream>
#include <shlobj.h>
#pragma comment(lib, "shell32.lib")
int main() {
CHAR my_documents[MAX_PATH];
HRESULT result = SHGetFolderPathA(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, my_documents);
if (result != S_OK)
std::cout << "Error: " << result << "\n";
else
std::cout << "Path: " << my_documents << "\n";
return 0;
}
- 之前用网上的代码最后得到总是一个十六进制的数字。。因为我的环境支持Unicode,一些Windows接口默认是宽字符类型的,比如 SHGetFolderPath 默认是 SHGetFolderPathW,而我这需要ascii,所以应该用 SHGetFolderPathA。
- CSIDL_PERSONAL 表示“我的文档”,你还可以换其他的参数来获取Windows下的特殊目录。