1

I have successfully made a directory in AppData Folder, but i want to navigate into that folder using C++ How do i go about it.

My code looks like this

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

int main()
{
    //printf("Hello world!\n");

    char *name = getenv("USERNAME");
    char info[1500];
    const int bufferSize = MAX_PATH;
    sprintf(info,"C:\\Users\\%s\\AppData\\Local\\BizMail", name);
    _mkdir(info);
    getchar();
    return 0;
}
MSalters
  • 173,980
  • 10
  • 155
  • 350
Shannaz
  • 9
  • 3
  • 1
    What do you mean by "navigate"? Change the current directory? (You should ask SHGetKnownFolderPath() for the AppData path btw) – Alex K. Sep 07 '17 at 12:11
  • _chdir would do it. – Samer Tufail Sep 07 '17 at 12:11
  • @AlexK. do something like this cd \C:\\Users\\Shannaz\\AppData\\Local\\BizMail – Shannaz Sep 07 '17 at 12:11
  • This method is unreliable. Use [SHGetKnownFolderPath](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762188(v=vs.85).aspx) and `FOLDERID_LocalAppData`. – molbdnilo Sep 07 '17 at 12:15
  • @Shannaz Can you explain why do you need to navigate.. not clear from your code – Rushikesh Deshpande Sep 07 '17 at 12:16
  • @RushikeshDeshpande, i want to see the contents in the Directory. – Shannaz Sep 07 '17 at 12:17
  • @Shannaz You can always use functions to find contents in directory until unless you actually want your process to be running in that particular path .. take a look https://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c – Rushikesh Deshpande Sep 07 '17 at 12:20
  • @RushikeshDeshpande now i want to Execute an image from the same directory, did i get it somewhat correctly? – Shannaz Sep 07 '17 at 12:50
  • Better use char folder_path[MAX_PATH]; ::SHGetFolderPathA(nullptr, CSIDL_LOCAL_APPDATA, nullptr, 0, folder_path); to retrieve the folder path of local app data. – Asesh Sep 07 '17 at 13:51

1 Answers1

1

Use chdir() function it works on both POSIX and Windows. Here is the man page

You can also use SetCurrentDirectory() function. Refer here and here is the sample program.

Rathan Naik
  • 993
  • 12
  • 25
  • i made a few Edits now have a look at the program now – Shannaz Sep 07 '17 at 12:44
  • 1
    @Shannaz: Please don't rewrite the entire question after it's been answered. This was a valid answer, but it stopped making sense after you changed the question. If you're go a new problem, check if we already have an answer to your problem (we've got literally thousands of C++ answers already). – MSalters Sep 07 '17 at 14:51