Introduction:

Here I will explain how to create directory or folder, delete directory or folder in asp.net using C# and VB.NET or Delete all files in directory.

 

To create or delete directory/folder we need to write the code like as shown below

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Create or Delete Directory/floder in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td><b>Enter File Name to Create:</b></td>
<td><asp:TextBox ID="txtName" runat="server" /></td>
<td> <asp:Button ID="btnCreate" runat="server" Text="Create Directory" Font-Bold="true" onclick="btnCreate_Click" /> </td>
</tr>
<tr>
<td><b>Enter File Name to Delete:</b></td>
<td><asp:TextBox ID="txtdltName" runat="server" /></td>
<td> <asp:Button ID="btnDelete" runat="server" Text="Delete Directory" Font-Bold="true" onclick="btnDelete_Click" /> </td>
</tr>
<tr><td colspan="3"><asp:Label ID="lblResult" runat="server" ForeColor="Red" /> </td></tr>
</table>
</form>
</body>
</html>
Now in code behind add the following namespaces

C# Code

using System;
using System.IO;
Once you add namespaces write the following code in code behind
// Create new directory
protected void btnCreate_Click(object sender, EventArgs e)
{
string strpath = @"D:\" + txtName.Text;
//Condition to check if any directory exists with same name
if (!(Directory.Exists(strpath)))
{
Directory.CreateDirectory(strpath);
lblResult.Text = "Directory Created";
}
else
{
lblResult.Text = "Already Directory Exists with the same name";
}
}
// Delete direcoty or folder
protected void btnDelete_Click(object sender, EventArgs e)
{
string strpath = @"D:\" + txtdltName.Text;
if (Directory.Exists(strpath))
{
RemoveDirectories(strpath);
}
else
{
lblResult.Text = "Directory not exists";
}
}
private void RemoveDirectories(string strpath)
{
//This condition is used to delete all files from the Directory
foreach (string file in Directory.GetFiles(strpath))
{
File.Delete(file);
}
//This condition is used to check all child Directories and delete files
foreach (string subfolder in Directory.GetDirectories(strpath))
{
RemoveDirectories(subfolder);
}
Directory.Delete(strpath);
lblResult.Text = "Directory deleted";
}
VB.NET Code

Imports System.IO
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As EventArgs)
End Sub
' Create new directory
Protected Sub btnCreate_Click(sender As Object, e As EventArgs)
Dim strpath As String = "D:\" + txtName.Text
'Condition to check if any directory exists with same name
If Not (Directory.Exists(strpath)) Then
Directory.CreateDirectory(strpath)
lblResult.Text = "Directory Created"
Else
lblResult.Text = "Already Directory Exists with the same name"
End If
End Sub
' Delete direcoty or folder
Protected Sub btnDelete_Click(sender As Object, e As EventArgs)
Dim strpath As String = "D:\" + txtdltName.Text
If Directory.Exists(strpath) Then
RemoveDirectories(strpath)
Else
lblResult.Text = "Directory not exists"
End If
End Sub
Private Sub RemoveDirectories(strpath As String)
'This condition is used to delete all files from the Directory
For Each file1 As String In Directory.GetFiles(strpath)
File.Delete(file1)
Next
'This condition is used to check all child Directories and delete files
For Each subfolder As String In Directory.GetDirectories(strpath)
RemoveDirectories(subfolder)
Next
Directory.Delete(strpath)
lblResult.Text = "Directory deleted"
End Sub
End Class
Demo

 

0 comments:

Post a Comment