L
L
logan baby2021-08-26 12:32:04
C++ / C#
logan baby, 2021-08-26 12:32:04

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

2 answer(s)
O
old2ev, 2021-08-26
@loganbaby

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);

In total, the code for Windows will look something like this:
#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)

}

For a Unix-like OS, getting a user looks like this:
#include <unistd.h>
char username[1024] = {0};
getlogin_r(username, sizeof(username)-1);

Therefore, the code for creating a directory will be like this:
#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)

}

Additional compiler/linker options may be required to use filesystem . GNU implementations prior to 9.1 (i.e. GCC) require linking with -lstdc++fs, and LLVM implementations prior to LLVM 9.0 (i.e. clang) require linking with -lc++fs.
Unix can be problematic if you run the program as root because its home directory is /root/ , and hence the desktop is /root/desktop/ . There may also be problems when directories in Linux have Russian names, for example /home/<username>/Desktop\ table, you need to somehow handle this moment, since GCC and clang at least know how to use Unicode. On Windows, there may be problems with MinGW compilers if the username is in Russian (or another language containing Unicode characters), because they can currently only use ASCII characters in paths, that is, only Latin characters. Here, either eat a cactus and throw from encoding to encoding, or use visual c ++. In general, good luck

L
lz961, 2021-08-28
@lz961

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 question

Ask a Question

731 491 924 answers to any question