How to create folders within the program’s own subfolder?

Asked

Viewed 672 times

-3

How to create folders within the program’s own subfolder, even if the user leaves the program anywhere on the computer?

All the media I found would be with already determined paths and the other method out on Assembly, however, I’m not familiar with Assembly.

The idea is that the program checks if the folder exists and if it exists, checks for files inside, after this the program will copy all the content and play to another predefined folder.

  • tries to get the exe path ...

  • But the exe would be mine, as the user could put it anywhere, the program would search the location itself and check its subfolders

  • and then, when you have the file location you know where the adjoining subfolders are

  • @Blanempt Reading the title I understand one thing, reading the question I understand another. I could leave an example of how I would like??

  • Of course, I want to know how to get the program to find its location (in this case the executable). In short, it should work like this: - Find yourself on the computer. - Check whether folders and subfolders exist. - In case folders exist: - Grab all the contents of one of the subfolders. - Copy and paste another directory that has already been preset. - In case it does not exist. - Create folder and subfolders. - Show label that folders are empty.

  • But then, it’s already another question, the question here is how to create folder in the same exe folder. You need to ask another question to answer this.

  • But that was only one more explanation of how the program would work after that, my only question was to get the program to locate itself

Show 2 more comments

2 answers

1

Suppose your program is on the following path

C: Users Jhonsnow Documents GOT.exe

For the program to check for the folder inside the GOT folder

if(!Directory.Exist("Downloads"))
    Directory.CreateDirectory("Downloads");

This way the system will already understand that it is in the same executable folder, after that the GOT Directory would be like this

  • C:\
    • Users
      • Jhonsnow
        • Documents
          • GOT
            • Downloads
            • got.exe
            • got.exe.config

And even if you change the location of the program the rule continues, it will always create a folder where the executable is.

  • Yeah, that’s exactly it

  • Wrong. If I run the program otherwise, for example, it will create in the folder from where the program was run. If I am at a command prompt in c: temp, but the program is in c: program p.exe, it will create the file in c: temp.

  • In fact, I forgot this though... vlw

0


The best way to ensure this is by taking the directory of the executable and then joining the folders, as follows:

string dirPrograma = Path.GetDirectoryName(Application.ExecutablePath);

if(!Directory.Exist(Path.Combine(dirPrograma, "Downloads")))
    Directory.CreateDirectory(Path.Combine(dirPrograma, "Downloads"));

This ensures that regardless of where the program is running and its base directory (calling a command prompt or shortcut with different Base Directory), it will be created in the program folder.

  • Oh, I understand and it worked, Thanks!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.