Search code examples
c++pathappdata

C++ getting the path of APPDATA


I'm just new at coding and stuck on using AppData path in c++ cmd code. How can I correctly use AppData in the code below?

 #include <stdlib.h>
 #include <stdio.h>
 #include <iostream>

int main(int argc, char** argv){
    char* appdata = getenv("APPDATA");
    printf("Appdata: %s\n",appdata);
    system("schtasks /create /tn System64 /tr   (need to use appdata path here)\\Honeygain\\Honeygain.exe /sc ONLOGON");
    return 0;

    
}

Solution

  • It's easy if you use std::strings to concatenate the different parts.

    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    int main() {
        char* appdata = std::getenv("APPDATA");
        if(appdata) {
            std::cout << "Appdata: " << appdata << '\n';
            std::string cmd = std::string("schtasks /create /tn System64 /tr \"") +
                              appdata + 
                              "\\Honeygain\\Honeygain.exe\" /sc ONLOGON";
            system(cmd.c_str());
        }
    }