Introduction: In this article I am going to explain how to delete all
files and sub folders from
folder i.e. delete everything inside specific folder in asp.net using both c# and VB language.
In previous articles i explained How to Delete specific type of files from folder and subfolders recursively and How to Bind,Save,Edit,Update,Cancel,Delete,Paging example in GridView in asp.net and bind,upload,download,delete image files from the GridView in asp.net and jQuery to Reset or Clear all Asp.Net or HTML controls placed on page and Server side validation code for all Asp.Net controls
Implementation: Let’s create an example
to demonstrate the concept.
Create a folder/directory with name “MyContents” in root directory. Add some
files and folders i.e sub folders in this folder. Also place some files and
folders in sub folders. Now we want to delete all the files and folders placed
in this folder and its sub folders.
A Directory that contains files or
folders cannot be deleted so we first need to deletes all the child directories
and also all the files in the directory and child directories.
Asp.Net C# Section
HTML Source
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClick="btnDelete_Click" />
</div>
</form>
</body>
</html>
Asp.Net
C# code to delete all files and
sub directories from directory
In
code behind file e.g. default.aspx.cs write the following:
First
of all include following required namespace:
using System.IO;
Then write the code as:
protected void btnDelete_Click(object sender, EventArgs e)
{
DirectoryInfo directory = new DirectoryInfo(Server.MapPath("~/MyContents/"));
EmptyFolder(directory);
}
private void EmptyFolder(DirectoryInfo directory)
{
foreach (FileInfo file in directory.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo subdirectory in directory.GetDirectories())
{
EmptyFolder(subdirectory);
subdirectory.Delete();
}
}
Asp.Net VB Section
HTML Source
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnDelete" runat="server" Text="Delete" />
</div>
</form>
</body>
</html>
Asp.Net
VB code to delete all files and
sub directories from directory
In
code behind file e.g. default.aspx.vb write the following:
First
of all include following required namespace:
Imports System.IO
Then write the code as:
Protected Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
Dim directory As New DirectoryInfo(Server.MapPath("~/MyContents/"))
EmptyFolder(directory)
End Sub
Private Sub EmptyFolder(directory As DirectoryInfo)
For Each file As FileInfo In directory.GetFiles()
file.Delete()
Next
For Each subdirectory As DirectoryInfo In directory.GetDirectories()
EmptyFolder(subdirectory)
subdirectory.Delete()
Next
End Sub
"A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..