Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Image my form
I'm working on something like antivirus program. I created tex document. And Wrote test, 123, asd. (After the scan its like virus for now.)
string[] listName = new String[] { "test", "123", "asd" };

After the scan this text document viewed on checkedlistbox. I want to select on checkedlistbox and delete from my computer. I tried the following code.
private void deleteViruses_btn(object sender, EventArgs e)
        {
            foreach (var item in checkedListBox1.CheckedItems.OfType<string>().ToList())
            {
                DialogResult dialogResult = MessageBox.Show("Do you want delete Founded Viruses?", "Delete", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    File.Delete(item);
                    checkedListBox1.Items.Remove(item);
                }
            }
            MessageBox.Show("Deleted");
            checkedListBox1.Refresh();
            
        }

It does not work. How can I delete this files after selected checkedlistbox?
Posted
Updated 1-Jun-15 13:15pm
v4
Comments
[no name] 1-Jun-15 21:24pm    
What is that you think "does not work" means? Did you step through this with your debugger?
Sergey Alexandrovich Kryukov 2-Jun-15 2:39am    
Do you really repeat asking the user in the loop? Big abuse...
—SA

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):
C#
internal struct ItemHelper {
   internal ItemHelper(string fileName) { // for example
       this.FileName = fileName;
   }
   internal string FileName { get; private set; }
   public override string ToString() {
      return this.FileName; // for example
      // or System.IO.Path.GetFileName(this.FileName);
   } 
   //...
} 

// confirmation dialog here
for (object item in myCheckedListBox.CheckedItems) { // nothing else!
    // no need in dynamic cast:
    ItemHelper itemHelper = (ItemHelper)item;
    string fileName = itemHelper.FileName;
    System.IO.File.Delete(fileName);
    // IMPORTANT! do not delete the item! 
}

// delete all checked items here, separately

[END EDIT]

That's all.

—SA
 
Share this answer
 
v2
Comments
Maciej Los 2-Jun-15 2:27am    
Hhmmm... I looked at image and there is a full file name, Sergey.
Sergey Alexandrovich Kryukov 2-Jun-15 2:42am    
Than it would work, in a wrong way though, because File.Delete is used. If it does not work, the argument to this call takes wrong value. Isn't it logical?

Now, the problem is asking the confirmation in the loop. This is totally wrong. The whole point of those check boxes is probably to mark what to delete. The confirmation should be asked only once.

My solution really solves the problem, if data is put correctly.

—SA
Maciej Los 2-Jun-15 3:04am    
I didn't say your answer is wrong... I wanted to point out that CheckedListBoxItem contains a full file name.
Sergey Alexandrovich Kryukov 2-Jun-15 10:00am    
All right, then something else is wrong.
Anyway, I'll update the answer.
—SA
There were red lines under the some codes. There were some errors in forms. And I fixed. It working. Thank you.
 
Share this answer
 

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