如何在文档文件夹中创建目录

How do I create a directory inside of the documents folder?

本文关键字:创建目录 文件夹 文档      更新时间:2023-10-16

这是我试图找到任何用户中的文档文件夹,然后创建一个文件夹。我对c++还比较陌生,只是想弄清楚目录是如何工作的

void useraccess::createtxt() {
//name is a pre-defined string
cout << "Creating usern";
#ifdef _WIN32
LPTSTR path = NULL;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &path);
if (SUCCEEDED(hr)) {
path + \name;
CreateDirectoryA(path, NULL);
}
else {
cout << "Error finding documents folder";
}
#elif __APPLE__
#else
cout << "Error";
#endif
}
#include <cstdlib>
#include <string>
#include <iostream>
#include <windows.h>
#include <shlobj_core.h>

int main()
{
PWSTR path_temp;
if (SHGetKnownFolderPath(FOLDERID_PublicDocuments, 0, nullptr, &path_temp) != S_OK) {
std::cerr << "SHGetKnownFolderPath() failed :(nn";
return EXIT_FAILURE;
}
std::wstring path{ path_temp };
CoTaskMemFree(path_temp);
path += L"\foobar";
if (SHCreateDirectory(nullptr, path.c_str()) != ERROR_SUCCESS) {
std::cerr << "SHCreateDirectory() failed :(nn";
return EXIT_FAILURE;
}
std::wcout << L"Directory "" << path << L"" created.nn";
}