How to Delete Files Directory Sub Directories in Asp.Net

Description:-

Files Directory is nothing but in computing, a directory is a file system cataloging structure which contains references to other computer files, and possibly other directories. A file can be fully and uniquely identified by its full name, including all directories to which it belongs. In this Example we will Delete File from File using File Directory in dotnet.

In your Webpage Drag Gridview controls so we can see all file easily. Files name, size, directory info and location actually where are the file. And in Column Create Link controls for Deleting file.

<div>
   <asp:GridView ID="gridviewDeleteFiles" runat="server" OnRowCommand="gridviewDeleteFiles_RowCommand" OnRowDeleting="gridviewDeleteFiles_RowDeleting">
      <Columns>
         <asp:ButtonField Text="Delete" CommandName="Delete" />
      </Columns>
   </asp:GridView>
</div>

Now go to Code behind and get all File Directory from Specified URL Code in Page Load so When First time page Call then get all files from that URL. Here I have Created Method and call in page load event.

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("~/C:/Users/Umesh/Desktop"));
  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();
}

In DirectoryInfo I have given my Desktop location so there I can delete file. Now generate Gridview Row Command event SO from Gridview we can delete that Particular from location.

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();
}

Now run your Webpage and there you can see files from Specified location. Now delete one file and that file also delete from original location.

Related Posts

Previous
Next Post »

Thanks for comments.....