In this article I will explain how to create and delete folder or directory in ASP.Net using C# and VB.Net. On many occasions we have requirement to create and delete folder or directory in ASP.Net Website Root Folder or Directory thus this article explains how we can achieve the same.
This article also covers the precautions to be taken while creating or deleting delete folder or directory in ASP.Net such as check whether the Folder (Directory) to be created exists, if yes then it cannot be recreated and a notification is displayed to the user. Same way if the Folder (Directory) to be deleted does not exists, if yes then it cannot be deleted and a notification is displayed to the user.
You might also like to read:
HTML Markup
The HTML Markup consists of an ASP.Net TextBox for allowing user to enter the Folder (Directory) name to be created or deleted and two Buttons one for creating the Folder (Directory) and one for deleting the Folder (Directory) respectively.
Folder Name:
<asp:TextBox ID="txtFolderName" runat="server" Text=""></asp:TextBox>
<br />
<asp:Button ID="btnCreate" runat="server" Text="Create Folder" OnClick="btnCreate_Click" />
<asp:Button ID="btnDelete" runat="server" Text="Delete Folder" OnClick="btnDelete_Click" />
Namespaces
You will need to import the following namespaces.
C#
VB.Net
Create and delete folder or directory in ASP.Net using C# and VB.Net
Below is the code for creating and deleting a folder (directory) in ASP.Net. First the path is generated using the folder (directory) Name from the TextBox. Here you will notice that we are using Server.MapPath this is an important function and is used since we need to create the folder (directory) within the ASP.Net Root Folder, if you do not use it then then the folder will be created in the ASP.Net Folder in the program files where the application is executed.
Creating a Folder (Directory)
While creating a folder (directory), first it checks whether the folder (directory) with the same name exists if no then the folder (directory) is created else user gets a JavaScript alert notification that the folder (directory) with the name already exists.
Deleting a Folder (Directory)
While deleting a folder (directory), first it checks whether the folder (directory) with the same name exists if yes then the folder (directory) is deleted else user gets a JavaScript alert notification that the folder (directory) with the name does not exists and hence cannot be deleted.
C#
protected void btnCreate_Click(object sender, EventArgs e)
{
string directoryPath = Server.MapPath(string.Format("~/{0}/", txtFolderName.Text.Trim()));
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Directory already exists.');", true);
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
string directoryPath = Server.MapPath(string.Format("~/{0}/", txtFolderName.Text.Trim()));
if (Directory.Exists(directoryPath))
{
Directory.Delete(directoryPath);
}
else
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Directory does not exist.');", true);
}
}
VB.Net
Protected Sub btnCreate_Click(sender As Object, e As EventArgs)
Dim directoryPath As String = Server.MapPath(String.Format("~/{0}/", txtFolderName.Text.Trim()))
If Not Directory.Exists(directoryPath) Then
Directory.CreateDirectory(directoryPath)
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Directory already exists.');", True)
End If
End Sub
Protected Sub btnDelete_Click(sender As Object, e As EventArgs)
Dim directoryPath As String = Server.MapPath(String.Format("~/{0}/", txtFolderName.Text.Trim()))
If Directory.Exists(directoryPath) Then
Directory.Delete(directoryPath)
Else
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Directory does not exist.');", True)
End If
End Sub
When a Directory is created and it already exists
When a Directory is deleted and it does not exists
Downloads