Reputation: 135
Here was my attempt at finding the document folder that is in any user and then creating a folder. I'm relatively new to c++ and just trying to figure out how directories work
void useraccess::createtxt() {
//name is a pre-defined string
cout << "Creating user\n";
#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
}
Upvotes: 0
Views: 293
Reputation:
#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 :(\n\n";
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 :(\n\n";
return EXIT_FAILURE;
}
std::wcout << L"Directory \"" << path << L"\" created.\n\n";
}
Upvotes: 2