How to Create or Remove Folders and Subfolders in Asp.Net C# and Vb.Net

← PrevNext →

You can easily create folders and subfolders at runtime in Asp.Net using methods in the Directory class of System.IO namespace. The methods that I am going to use in my example here are CreateDirectory() and Delete(), along with other methods, to show you how to create or remove folders (or directories) and subfolders (or subdirectories) in Asp.Net. The Code examples are written both in C# and in Vb.

Asp.Net CreateDirectory() Method

The CreateDirectory method of the Directory class creates a folder or a subfolder in a specified path. The method takes a parameter as path (the path of the folder).

Note: While using the CreateDirectory() method, you do not have to use the Directory.Exists() method. The Exists method checks if the folder or directory already exists. However, there’s no need to use the Exists method, since CreateDirectory() will create a folder only if the folder does exists in the specified path.

Code Behind (C#)
using System;
using System.IO;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string sFolderPath;
            sFolderPath = "D:/myPhotoAlbum";            // THE PATH OF THE FOLDER.

            Directory.CreateDirectory(sFolderPath);     // CREATE THE FOLDER (DIRECTORY).
        }
        catch
        {  
            //
        }
    }
}
Code Behind (Vb.Net)
Option Explicit On
Imports System.IO

Partial Class Site
    Inherits System.Web.UI.MasterPage

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            Dim sFolderPath As String = "D:/myPhotoAlbum"   ' THE PATH OF THE FOLDER.

            Directory.CreateDirectory(sFolderPath)          ' CREATE A FOLDER (DIRECTORY).
        Catch ex As Exception
            '
        End Try
    End Sub
End Class

Create Subfolders (or Subdirectories) using CreateDirectory() Method

Using the CreateDirectory method, you can also create Subfolders (or subdirectories). You just have the mention the names of the subfolders. For example, I wish to create a subfolder called kids under myPhotoAlbum. I’ll do this.

C#
string sFolderPath;
sFolderPath = "D:/myPhotoAlbum/kids";    // FOLDER AND SUBFOLDER.

Directory.CreateDirectory(sFolderPath);
Vb.Net
Dim sFolderPath As String = "D:/myPhotoAlbum/kids"
Directory.CreateDirectory(sFolderPath)

Remove or Delete Folders using Delete() Method

Removing or deleting of folders and subfolders can be done using the Delete() method of the Directory Class.

The Delete() method takes a parameter as path (the path of the folder).

Note: You must delete the subfolders first, before deleting a folder completely.

Code Behind (C#)
try
{
    string sFolderPath;
    sFolderPath = "D:/myPhotoAlbum/kids";    // THE PATH OF THE FOLDER.

    Directory.Delete(sFolderPath);          // DELETE THE FOLDER.
}
catch
{   // }

The above code will remove the specified folder. I have only mentioned the folder to delete.

However, if there are files or subfolders inside the folder myPhotoFolder, it will throw an error saying, The directory is not empty. Therefore, you must first delete all files and subfolders (if any), before deleting the main folder.

Code Behind (Vb.Net)
Try
    Dim sFolderPath As String = "D:/myPhotoAlbum"   ' THE PATH OF THE FOLDER.
    Directory.Delete(sFolderPath)                   ' DELETE THE FOLDER.
Catch ex As Exception
    '
End Try

Using GetCreationTime() Method to Get the date and time of Creation of Folders

There are other methods that you can use while creating or deleting folders. For example, you can know the date and time when the folders or the subfolders were created.

The GetCreationTime() method of Directory class provides accurate date and time when the folders and subfolders were created. You can use the method anytime after the creation of the folder.

The method GetCreationTime() takes a parameter as path (the full path of the folder and subfolders).

C#
try
{
    string sFolderPath;
    sFolderPath = "D:/myPhotoAlbum/kids";

    Directory.CreateDirectory(sFolderPath);

    // GET THE DATE AND TIME OF CREATION OF FOLDER.
    msg.InnerHtml = Directory.GetCreationTime(sFolderPath).ToString();
}
catch
{  }

The msg in the above code, is a <p> element to show the date and time when the subfolders was created.

Vb.Net
Try
    Dim sFolderPath As String = "D:/myPhotoAlbum/kids"

    Directory.CreateDirectory(sFolderPath)

    ' GET THE DATE AND TIME OF CREATION OF FOLDER.
    msg.InnerHtml = Directory.GetCreationTime(sFolderPath)
 Catch ex As Exception
    '
 End Try

Note: Always make sure that you have the necessary permissions to create or remove folders and subfolders, on the servers specifically.

← PreviousNext →