Create Folders C #

-1

I would like to do the following.

Create three folders within each other in "c:/" . Their names will be designated by textboxes and in the last, create a .doc file by taking the values of textboxes .

It's for windows forms application.

I've tried:

string folder = @"C:\folder"; //nome do diretorio a ser criado
folder = textBox1.Text;
//Se o diretório não existir...

if (!Directory.Exists(folder))
{

 //Criamos um com o nome folder
 Directory.CreateDirectory(folder);

}

My question is how to create folders inside the others! One I was able to create. It also needs to be on a specific path @"C:\folder" . Thank you in advance!

    
asked by anonymous 24.06.2016 / 22:47

1 answer

3

To create the directories, just create the last directory that the other levels are created automatically, you can use the Directory.CreateDirectory() method, as shown below:

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Specify the directory you want to manipulate.
        string path = @"c:\MyDir1\MyDir2\MyDir3\";

        try 
        {
            // Determine whether the directory exists.
            if (Directory.Exists(path)) 
            {
                Console.WriteLine("That path exists already.");
                return;
            }

            // Try to create the directory.
            DirectoryInfo di = Directory.CreateDirectory(path);
            Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path));

            // Delete the directory.
            di.Delete();
            Console.WriteLine("The directory was deleted successfully.");
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        } 
        finally {}
    }
}

Example extracted from the Microsoft documentation: link

    
24.06.2016 / 22:51