0

I have a ListBox which contain and display individual xml files. The archived files are accumulating and I need to implement a delete button to permanently delete the selected xml files.enter image description here

The files are stored within the folder named “Archive” folder (please see screenshot) enter image description here.
How do I implement the solution? I have the following code behind for my delete button. The code is working by deleting the records within the memory, however it couldn’t delete the actual file, each time the page loads the files are still there. I appreciate your help. Thanks.

    protected void DeleteValues(object sender, EventArgs e)
   {
       List<ListItem> deletedItems = new List<ListItem>();
       foreach (ListItem item in ListBoxArchive.Items)
       {
           if (item.Selected)
           {
               deletedItems.Add(item);
           }
       }

       foreach (ListItem item in deletedItems)
       {
          ListBoxArchive.Items.Remove(item);

       }
   }

3 Answers3

0

You need to add System.IO for File Manipulation. Add this Method to your class

using System.IO

string FolderLocation = @"c:\MyFolder";
public void DeleteItemFile(string FileName){
    string fileLPath = Path.Combine(FolderLocation,FileName);
    File.Delete(filePath);


}

Now Modify your foreach loop add this one line.

foreach (ListItem item in deletedItems)
   {
      ListBoxArchive.Items.Remove(item);
      DeleteItemFile(item.ToString());

   }

This will only work if the ListItem that you have is the exactfilename in the list.

Make sure that the ListItem (item) has the file extension as well or else you have to concatinate it.

Aizen
  • 1,807
  • 2
  • 14
  • 28
  • Thanks Aizen, the selected files inside the listbox and "Archive" folder are now deleted. For some reason, when I load the page, the deleted files are still posted inside the ListBox, weird because it's already deleted the files from the Archive folder. Here is my code for Page_Load and it's calling a method called loadXMLFiles(). Can you please let me know what's wrong with the loadXMLFiles method.

    https://codeshare.io/aWnMF
    –  Jan 23 '16 at 21:35
  • after calling the Delete Method. You should free the FileInfo[] array. Meaning the reference and data is still there but the files is already deleted. So after Delete Method from the Actual file you need to inFiles = null; Now put a try and catch from foreach itteration since you are going to null the array. – Aizen Jan 25 '16 at 00:37
  • do the same from the outFiles – Aizen Jan 25 '16 at 00:38
0

There are two factors that you need to accomplish this. The modification to your code, like this:

    protected void DeleteValues(object sender, EventArgs e)
    {
        List<ListItem> deletedItems = new List<ListItem>();
        foreach (ListItem item in lbItems.Items)
        {
            if (item.Selected)
            {
                deletedItems.Add(item);
            }
        }
        String ArchiveFolderPath = Server.MapPath("/Archive/");
        foreach (ListItem item in deletedItems)
        {
            lbItems.Items.Remove(item);
            System.IO.File.Delete(ArchiveFolderPath + item.Text);//assumes item.Text is a valid file name

        }
    }

The next thing you need to do is give the windows account write permissions to the folder on the web server.

While that answers your programming question, I would like to suggest that you handle the file housekeeping through some scheduled task that maybe runs a powershell script.

Delete files older than 15 days using PowerShell

Community
  • 1
  • 1
Paul
  • 176
  • 3
  • 16
0

You can do this by using a single looping as follows:

string BaseFolder = @"folderLocation here";
foreach (ListItem item in ListBoxArchive.Items)
   {
       if (item.Selected)
       {
           string fileLPath = Path.Combine(BaseFolder,item.ToString());
           File.Delete(filePath);
       }
   }
   //Rebind the List here
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88