Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

C# - How to Delete all Files in Folders and SubFolders in C#, VB.NET

Apr 10, 2013
Introduction:

Here I will explain how to delete all files in folders and subfolders in asp.net using C# and VB.NET.

Description:

In previous articles I explained Create or Delete directory/folder in c#, Delete files from uploaded folder in asp.net, jQuery Dropdown Menu example, start and stop timer example in JavaScript, Asp.net Interview questions and many articles relating to Gridview, SQL, jQuery,asp.net, C#,VB.NET. Now I will explain how to delete files in folders and subfolders in asp.net using C# and VB.NET.

To delete files in folders and subfolders first we need to add namespace using System.IO

Once we add namespace need to write the code like as shown below

C# Code


// Delete direcoty or folder
protected void btnDelete_Click(object sender, EventArgs e)
{
string strpath = @"D:\" + txtdltName.Text;
if (Directory.Exists(strpath))
{
RemoveDirectories(strpath);
}
}
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);
}
VB.NET Code


' 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)
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
If you want to see complete example check below url



If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

Unknown said...

thanks for this c# code

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.