1

i have this code to create subfolders in the path the user choose

FolderBrowserDialog folderBrs = new FolderBrowserDialog();

        if (folderBrs.ShowDialog() == DialogResult.OK)
        {
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderBrs.SelectedPath;

            dir.CreateSubdirectory("subfolder1");
            dir.CreateSubdirectory("subfolder2");

        } 

it works fine, but the problem is ir makes the subfolders without the principal folder, so y tried this code

FolderBrowserDialog folderBrs = new FolderBrowserDialog();

        if (folderBrs.ShowDialog() == DialogResult.OK)
        {
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(folderBrs.SelectedPath + textBox1.Text.Trim());

            dir.CreateSubdirectory("subfolder1");
            dir.CreateSubdirectory("subfolder2");

        } 

as you can see, the only diference is the textBox addition, but when i type the name it doesn't create the folder, it doesn't anything, but the curios thing is, if i choose an existent folder it creates the folder with the subfolders, but the name of the principal folder is mixed with the name of the existent folder that i choosed

what i am doing wrong? any suggestion?

2 Answers2

1

Here is the code corrected, assuming the selected path exists:

if ( folderBrs.ShowDialog() == DialogResult.OK )
{
  var dir = new DirectoryInfo(folderBrs.SelectedPath);
  dir = dir.CreateSubdirectory(textBox1.Text.Trim());
  dir.CreateSubdirectory("subfolder1");
  dir.CreateSubdirectory("subfolder2");
}

We take an instance of a directory info for the selected path.

Next we create the subfolder from the textbox.

Then we create the two subfolders in it.

  • i am using a winfows form, thats why i am using a textbox, if i type only **textbox** it makes an error. what i wanted to do it's create a folder, and the user types the name, then inside that folder create the subfolders – Jesus David chavero martinez Jul 14 '20 at 18:11
  • i tried your code, but for some reason it creates the folder in the desktop, but first i have to choose another existent folder, then it creates the folder with the names mixed :( – Jesus David chavero martinez Jul 14 '20 at 18:14
  • Try that and tell me if it works or not: [create-folder-with-subfolders-in-any-path.zip](https://www.ordisoftware.com/files/stack-overflow/create-folder-with-subfolders-in-any-path.zip) –  Jul 14 '20 at 18:18
  • yes, this is exactly what i wanted to do. but the only detail is it create the subfolder, inside the sub folder inside the principal folder like this folder>subfolder1>subfolder2, do you know how to make it folder>subfolder1 + subfolder2? – Jesus David chavero martinez Jul 14 '20 at 18:20
  • 1
    sorry, it is dificult explain it. it's not concatenates, its just the two subfolders inside th eprincipal folder instead a subfolder, inside the subfolder, inside the principal subfolder :P – Jesus David chavero martinez Jul 14 '20 at 18:33
1

Let's create the required path as a string:

string dir = Path.Combine(folderBrs.SelectedPath + textBox1.Text.Trim(),
  "subfolder1",
  "subfolder2");

Then we can create the directory:

Directory.CreateDirectory(dir);

Sure, you can combine both fragments into one:

using System.IO;

...

using (FolderBrowserDialog folderBrs = new FolderBrowserDialog()) {
  if (folderBrs.ShowDialog() == DialogResult.OK) 
    Directory.CreateDirectory(Path.Combine(
      folderBrs.SelectedPath + textBox1.Text.Trim(),
      "subfolder1",
      "subfolfer2" 
    ));   
}

    
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215