Delete specific type of files from folder and subfolders recursively in Asp.net C#,VB

Introduction:  In this article I am going to explain how to delete certain type of files i.e. all the files with specific extension (for example .jpg file or .png file or .docx file etc) from specific directory and its subdirectories recursively in asp.net using both C# and VB language.


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 in this folder. Also place some files and folders in sub folders. Now let’s say we want to delete all “.jpg“ files from “MyContent” folders and its sub folders.

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 subdirectories 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())
        {
            if (file.Extension.ToLower().Equals(".jpg"))
            {
                file.Delete();
            }
        }
        foreach (DirectoryInfo subdirectory in directory.GetDirectories())
        {
            EmptyFolder(subdirectory);           
        }
    }
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 subdirectories 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()
            If file.Extension.ToLower().Equals(".jpg") Then
                file.Delete()
            End If
        Next
        For Each subdirectory As DirectoryInfo In directory.GetDirectories()
            EmptyFolder(subdirectory)
        Next
    End Sub

Now over to you:
"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."
Previous
Next Post »

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..