Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i created one windows application in that i want to delete the temporary files i can delete the files from listview but i can't delete files from the system it showing again same files in the system
this is my code under delete
C#
private void button1_Click(object sender, EventArgs e)
{
    var tmpPath = Environment.GetFolderPath(Environment.SpecialFolder.Recent);

    string[] files = Directory.GetFiles(tmpPath, "*.*", SearchOption.AllDirectories);

    listView1.Items.AddRange((files.Select(f => new ListViewItem(f))).ToArray());
}

private void btndelete_Click(object sender, EventArgs e)
{
    if (listView1.SelectedItems.Count > 0)
    {
        var confirmation = MessageBox.Show("Do You Really Want to Delete the Item", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (confirmation == DialogResult.Yes)
        {
            for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
            {
                ListViewItem itm = listView1.SelectedItems[i];
                listView1.Items[itm.Index].Remove();
            }
        }
        else
            MessageBox.Show("None Selected");
    }
}
Posted
Updated 18-Dec-14 22:09pm
v5
Comments
Tomas Takac 19-Dec-14 3:45am    
What does "can't delete from the system" actually mean? Do you get an exception? Wait, there is no code for deleting files there. Removing them form the list is not enough, you need to call File.Delete()[^].
Srikanth59 19-Dec-14 4:07am    
temporary files from the system i got the files in the listview of the application but am unable to delete from the temporary folder of the system

Tomas Takac 19-Dec-14 4:12am    
Again, you are not deleting the files at all. Not in the code you are showing. If you want a file deleted you just have to delete the file. See solution #1 for how to do it.
Richard MacCutchan 19-Dec-14 4:41am    
You need to add the code that does the actual delete of each file. There is no link between the ListView and the files on the system, it is just a collection of strings.

1 solution

In your code you are just removing the items from the listview, but you are not deleting anything. You must use File.Delete, something like this:

C#
if (listView1.SelectedItems.Count > 0)
{
var confirmation = MessageBox.Show("Do You Really Want to Delete the Item", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (confirmation == DialogResult.Yes)
{
for (int i = listView1.SelectedItems.Count - 1; i >= 0; i--)
{
ListViewItem itm = listView1.SelectedItems[i];

File.Delete(itm.Text.ToString()); 

listView1.Items.Remove(itm);
}
 
}
else
MessageBox.Show("None Selected");
}
 
Share this answer
 
v3
Comments
Srikanth59 19-Dec-14 3:59am    
i get the files into listview as
var tmpPath = Environment.GetFolderPath(Environment.SpecialFolder.Recent);

string[] files = Directory.GetFiles(tmpPath, "*.*", SearchOption.AllDirectories);

listView1.Items.AddRange((files.Select(f => new ListViewItem(f))).ToArray())
Pikoh 19-Dec-14 5:53am    
See updated solution. Adding the bolded line should do the job.
Srikanth59 22-Dec-14 0:07am    
thank you for your kindly information

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900