Here I will explain how to delete all files in directories and subdirectories in asp.net using C# and VB.NET.
In previous articles I explained Create or Delete directory/folder in c#, Delete files from uploaded folder in asp.net, Check username availability with progressbar, Asp.net Interview questions, Joins in SQL Server and many articles relating to Gridview, SQL ,jQuery,asp.net, C#,VB.NET. Now I will explain how to delete all directories and subdirectories in asp.net using C# and VB.NET.
To delete all directories and subdirectories first we need to add namespace using System.IO Once we add namespace need to write the code like as shown below
// Delete direcoty or folder
protected void btnDelete_Click(object sender, EventArgs e)
string strpath = @"D:\" + txtdltName.Text;
private void RemoveDirectories(string strpath)
//This condition is used to delete all files from the Directory
foreach (string file in Directory.GetFiles(strpath))
//This condition is used to check all child Directories and delete files
foreach (string subfolder in Directory.GetDirectories(strpath))
' 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
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)
'This condition is used to check all child Directories and delete files
For Each subfolder As String In Directory.GetDirectories(strpath)
lblResult.Text = "Directory deleted"
If you want to see complete example check below url