Create Folder using C#

With the use of the following code you can give the user the ability to enter a directory name, check to see if it already exists and if not then create the folder and upload the files there.

This article provides a few steps to create a folder which will be easy to follow.

Step 1

Design a form, then drag a TextBox from the toolbox, and then drag a Lable and a button; give the button the caption Create Folder. It should look like this:

folder.gif

Step 2

For the Create Folder button's Click event write the following code:

protected void Button1_Click(object sender, EventArgs e)
{
    if (!System.IO.Directory.Exists(MapPath(MyTree.SelectedValue + "\\" + TextBox1.Text)))
    {          
        System.IO.Directory.CreateDirectory(MapPath(TextBox1.Text));
        Label1.Text = "Directory Created Successfully..........";
        TextBox1.Text = "";    
    }
    else
    {
        Label1.Text = "Directory Already Exist ..........";
    }
}

Note: To create subfolder write this code

System.IO.Directory.CreateDirectory(MapPath(Foldername+ "\\" + TextBox1.Text));
Label1.Text = "Directory Created Successfully..........";
TextBox1.Text = "";

Foldername is the name of the folder in which you want to create subfolder.

Run your website........


Similar Articles