C++ 获取 Documents 目录的时候如果是中文系统怎么获取
时间: 2024-05-11 11:20:26 浏览: 6
在 Windows 系统中,Documents 目录的路径可以通过 `SHGetFolderPath` 函数获取,在中文系统中,Documents 目录的名称为“文档”,因此需要指定 `CSIDL_PERSONAL` 和 `SHGFP_TYPE_CURRENT` 参数来获取当前用户的文档目录路径,具体实现如下:
```c++
#include <windows.h>
#include <shlobj.h>
#include <iostream>
#include <string>
std::wstring GetDocumentsPath()
{
wchar_t path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, path))) {
return std::wstring(path) + L"\\";
}
return L"";
}
int main()
{
std::wcout << GetDocumentsPath() << std::endl;
return 0;
}
```
上述代码中,`SHGetFolderPathW` 函数的第二个参数 `CSIDL_PERSONAL` 表示获取当前用户的个人文件夹路径,第四个参数 `SHGFP_TYPE_CURRENT` 表示获取当前用户的路径。函数返回的路径为 `wchar_t` 类型,需要将其转换为 `std::wstring` 类型,并在结尾加上路径分隔符“\”。