Create C#Folders

Asked

Viewed 4,402 times

-1

I’d like to do the following.

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


It is for windows Forms application.

I tried to:

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 one folder inside another! One I managed to create. It also needs to be in a specific path @"C:\folder". Thank you in advance!

  • 1

    Post what you’ve done and what is your doubt.

  • 2

    What type of project? ASP.NET Web Forms? MVC? Windows Forms?

1 answer

4

To create the directories, just create the last directory that the other levels are created automatically, you can use the method Directory.CreateDirectory(), 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 taken from the Microsoft documentation: https://msdn.microsoft.com/en-us/library/54a0at6s(v=vs.110). aspx

  • How it create the 3 folder ai designated by textboxes and at last create a doc file ?

  • The above code is about creating the folders, now about creating the doc, @Rafael Ferreira has to post how he will create the document, because there are numerous ways to do this, I think the ideal is he create another question about how to create the file . doc.

Browser other questions tagged

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