Delete Files Directory Sub Directories From Server In Asp.Net

This example explains How To Delete Files Folders Directory And Sub Directories From Server In Asp.Net Using C# And VB.NET

I have placed one GridView on the aspx page to Display Directory or Contents of SubDirectory from server and have added one ButtonField In GridView Source for deletion when user clicks on button.

I have created one DataTable which is getting data with help of DirectoryInfo class.

I'm Deleting Files in RowCommand usingSystem.IO.File.Delete and Directories or folders using System.IO.Directory.Delete methods.

Delete Files Folders Directories From Server Directory In Asp.Net

HTML SOURCE OF GRIDVIEW
   1:  <asp:GridView ID="gridviewDeleteFiles" runat="server" 
   2:         onrowcommand="gridviewDeleteFiles_RowCommand" 
   3:         onrowdeleting="gridviewDeleteFiles_RowDeleting">
   4:  <Columns>
   5:  <asp:ButtonField Text="Delete" CommandName="Delete"/>
   6:  </Columns>
   7:  </asp:GridView>

C# CODE
using System.Data;
using System.IO;
protected void Page_Load(object sender, EventArgs e)
    {
       FillGridView();
    }
private void FillGridView()
    {
        DataTable dtGridViewSource = new DataTable();
        dtGridViewSource.Columns.Add(new DataColumn("Name", typeof(System.String)));
        dtGridViewSource.Columns.Add(new DataColumn("Size", typeof(System.String)));
        dtGridViewSource.Columns.Add(new DataColumn("Type", typeof(System.String)));
        dtGridViewSource.Columns.Add(new DataColumn("Path", typeof(System.String)));
        DataRow gridviewRow;

        //Get All Folders Or Directories and add in table
        DirectoryInfo directory = new DirectoryInfo(Server.MapPath("~/csharpdotnetfreak.blogspot.com"));
        DirectoryInfo[] subDirectories = directory.GetDirectories();
        foreach (DirectoryInfo dirInfo in subDirectories)
        {
            gridviewRow = dtGridViewSource.NewRow();
            gridviewRow["Name"] = dirInfo.Name;
            gridviewRow["Type"] = "Directory";
            gridviewRow["Path"] = dirInfo.FullName;
            dtGridViewSource.Rows.Add(gridviewRow);
        }
        //Get files in all directories 
        FileInfo[] files = directory.GetFiles("*.*", SearchOption.AllDirectories);
        foreach (FileInfo fileInfo in files)
        {
            gridviewRow = dtGridViewSource.NewRow();
            gridviewRow["Name"] = fileInfo.Name;
            gridviewRow["Size"] = fileInfo.Length;
            gridviewRow["Type"] = "File";
            gridviewRow["Path"] = fileInfo.FullName;
            dtGridViewSource.Rows.Add(gridviewRow);
         }
        gridviewDeleteFiles.DataSource = dtGridViewSource;
        gridviewDeleteFiles.DataBind();
    }

protected void gridviewDeleteFiles_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            GridViewRow row = gridviewDeleteFiles.Rows[Convert.ToInt32(e.CommandArgument)];
            string fileName = row.Cells[1].Text;
            string filePath = row.Cells[4].Text;
            string type = row.Cells[3].Text;
            if (type == "File")
            {
                System.IO.File.Delete(filePath);
            }
            else if (type == "Directory")
            {
                System.IO.Directory.Delete(filePath, true);

            }
        }
        FillGridView();
    }

VB.NET CODE
Protected Sub Page_Load(sender As Object, e As EventArgs)
 FillGridView()
End Sub

Private Sub FillGridView()
 Dim dtGridViewSource As New DataTable()
 dtGridViewSource.Columns.Add(New DataColumn("Name", GetType(System.String)))
 dtGridViewSource.Columns.Add(New DataColumn("Size", GetType(System.String)))
 dtGridViewSource.Columns.Add(New DataColumn("Type", GetType(System.String)))
 dtGridViewSource.Columns.Add(New DataColumn("Path", GetType(System.String)))
 Dim gridviewRow As DataRow

 'Get All Folders Or Directories and add in table
 Dim directory As New DirectoryInfo(Server.MapPath("~/csharpdotnetfreak.blogspot.com"))
 Dim subDirectories As DirectoryInfo() = directory.GetDirectories()
 For Each dirInfo As DirectoryInfo In subDirectories
  gridviewRow = dtGridViewSource.NewRow()
  gridviewRow("Name") = dirInfo.Name
  gridviewRow("Type") = "Directory"
  gridviewRow("Path") = dirInfo.FullName
  dtGridViewSource.Rows.Add(gridviewRow)
 Next
 'Get files in all directories 
 Dim files As FileInfo() = directory.GetFiles("*.*", SearchOption.AllDirectories)
 For Each fileInfo As FileInfo In files
  gridviewRow = dtGridViewSource.NewRow()
  gridviewRow("Name") = fileInfo.Name
  gridviewRow("Size") = fileInfo.Length
  gridviewRow("Type") = "File"
  gridviewRow("Path") = fileInfo.FullName
  dtGridViewSource.Rows.Add(gridviewRow)
 Next
 gridviewDeleteFiles.DataSource = dtGridViewSource
 gridviewDeleteFiles.DataBind()
End Sub

Protected Sub gridviewDeleteFiles_RowCommand(sender As Object, e As GridViewCommandEventArgs)
 If e.CommandName = "Delete" Then
  Dim row As GridViewRow = gridviewDeleteFiles.Rows(Convert.ToInt32(e.CommandArgument))
  Dim fileName As String = row.Cells(1).Text
  Dim filePath As String = row.Cells(4).Text
  Dim type As String = row.Cells(3).Text
  If type = "File" Then
   System.IO.File.Delete(filePath)
  ElseIf type = "Directory" Then

   System.IO.Directory.Delete(filePath, True)
  End If
 End If
 FillGridView()
End Sub

Download Sample Code


If you like this post than join us or share

2 comments:

Anonymous said...

Nice One Dude


Anonymous said...

thank you for this great code,
is there a way to put confirm geleting message before the delete command ???


Find More Articles