Click here to Skip to main content
15,888,121 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My previous question has not been answered correctly and still I have looked for the correct answer to my problem with no luck. My code does not delete the checklistbox item when my scanner finds the file I wish to delete. The item is removed from the checklistbox but the actual file still remains in the computer which defeats my purpose of scanning in the first place.

The delete code:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  For i As Integer = CheckedListBox1.CheckedItems.Count - 1 To 0 Step -1
  red = Replace(CheckedListBox1.CheckedItems.Item(i).ToString(),"Threat Found: ","")            
          Try
          System.IO.File.Delete(red)
          If Dir(red)<> "" Then
   MsgBox("Unable to delete"& red, MsgBoxStyle.Critical)
          Else
   MsgBox(String.Format("File {0} successfully deleted", red), MsgBoxStyle.Critical)
   CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems)
          End If
        Catch ex As Exception
          End Try
        Next
        While CheckedListBox1.CheckedItems.Count > 0
        CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems(0))
        End While
    End Sub


I figure something small might be missing but to me everything seems fine. Can someone explain or provide a working correction to this code as I have been stuck for some time with no idea what went wrong.
Posted
Updated 2-Nov-10 20:20pm
v2
Comments
Dale 2012 3-Nov-10 2:47am    
I have made a self observation about the code and noticed that I do not need:

While CheckedListBox1.CheckedItems.Count > 0
CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems(0))
End While

but I am still trying to delete the file itself with no luck....

1 solution

Before calling System.IO.File.Delete(red), call System.IO.File.Exists(red) to make sure the file path is valid. Only call Delete if Exists returns True, otherwise show an error message box, so you know that you did not get the proper file path.
 
Share this answer
 
Comments
Dale 2012 4-Nov-10 1:24am    
I have changed the code to imply what you have said but it still does not work....
Dale 2012 4-Nov-10 1:26am    
I took out the try catch statement and added a if statement to the code but it still only removes the name from the checklistbox and not the actual file. The file path exists because I get no error when trying to delete it but I watch to see if it is gone from the folder I placed it in and it does not...... Please help

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