Answer the question
In order to leave comments, you need to log in
How to create a folder on the desktop using c++?
How to create a folder on the desktop using c++?
Answer the question
In order to leave comments, you need to log in
The c++17 version has a filesystem library that allows you to do some manipulations with the computer's file system. As for your question, there is a function like bool create_directory(...) . This function takes as an argument a path whose end is the name of the folder to create.
On Windows, the desktop is located along the path:
/users/<username>/desktop/
On Unix-like (BSD, Linux, etc.) along the path:
/home/<username>/desktop/
And then we need to get the username :
On Windows it's something like this:
#include <windows.h>
#include <Lmcons.h>
char username[UNLEN+1]; // <-- сюда запишется имя пользователя
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);
#include <windows.h>
#include <Lmcons.h>
#include <filesystem>
#include <string>
namespace fs = std::filesystem; // Для краткости
bool createDesktopDir(std::string dir_name) {
char username[UNLEN+1];
DWORD username_len = UNLEN+1;
GetUserName(username, &username_len);
return fs::create_directory(std::string("/users/") + username + "/desktop/" + dir_name)
}
#include <unistd.h>
char username[1024] = {0};
getlogin_r(username, sizeof(username)-1);
#include <unistd.h>
#include <filesystem>
#include <string>
namespace fs = std::filesystem;
bool createDesktopDir(std::string dir_name) {
char username[1024] = {0};
getlogin_r(username, sizeof(username)-1);
return fs::create_directory(std::string("/home/") + username + "/desktop/" + dir_name)
}
Windows
#include <cstdlib>
#include <string>
#include <direct.h>
int createDescktopDir(const char* dname) {
// корректная обработка ошибок за вами
const char* env_homedrive = std::getenv("HOMEDRIVE");
const char* env_homepath = std::getenv("HOMEPATH");
std::string desctoppath = std::string(env_homedrive) + env_homepath + "\\Desktop\\" + dname;
return _mkdir(desctoppath.c_str());
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question