Your list items should be the full names of the infected files. You don't have to show the full names in UI. To achieve that, you can create an item class (or
struct
) with all item information you need for your work and override
System.Object.ToString
. You will find more detail in my past answers:
combobox.selectedvalue shows {} in winform[
^],
Windows Form ListBox Selections[
^].
For selected items, typecast each to your
class/struct
, extract the file name from appropriate property used to access the file name, use this value in
System.IO.File.Delete
.
[EDIT]
The major mistake is using the confirmation dialog inside the loop. As I understand, the indication of the file to be deleted should be the checked state of the item. So, you can use just on dialog before the loop.
You need to run the loop through all the items without "of type", without "to list" (I always use
TreeView
instead, due to style problems of the list box):
internal struct ItemHelper {
internal ItemHelper(string fileName) {
this.FileName = fileName;
}
internal string FileName { get; private set; }
public override string ToString() {
return this.FileName;
}
}
for (object item in myCheckedListBox.CheckedItems) {
ItemHelper itemHelper = (ItemHelper)item;
string fileName = itemHelper.FileName;
System.IO.File.Delete(fileName);
}
[END EDIT]
That's all.
—SA