Set a breakpoint on the foreach line and use the debugger and look at the value of deleteFiles. It does not contain what you think it does.
Your logic is completely screwed up. Why are you calling ToString on CheckedItems? That's the very collection you're iterating over in the
foreach
. That's pointless. You're completely ignoring the iterator you setup over the collection,
item
.
Item is going to hold each checked item in the CheckedItems collection, allowing you to deal with one item in the collection at a time. You should be calling .ToString() on
item
. So long as it's holding the string representing a filepath, you should be passing that to File.Delete().
private void bunifuFlatButton4_Click(object sender, EventArgs e)
{
foreach (var item in checkedListBox1.CheckedItems)
{
string filepath = item.ToString();
File.Delete(filepath);
}
}
Learn to use the debugger. It's there to show you what the code is doing. Chances are really high that you think the code is doing something and it's actually doing something else.
The debugger is there to debug YOU and your understanding of the code.