Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have posted this question now a few times with few answers that have given me any progress in this situation. My program scans for names that are within a text file and if the name in the text file is found the result then becomes a item in my checklistbox. The problem is that my code only removes the item and not the actual file in my computer.


my code is as follows:

VB
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.Information)
         CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems)
                End If
            Catch ex As Exception
            End Try
        Next
end sub




Again I have had someone say that I should see if the file exists by saying System.io.file.exists(red) and then going onto system.io.file.delete(red) and if true to delete or show a msgbox error.

This is a good idea and I have tried to change the code to this.....

VB
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: ","")
            If System.IO.File.Exists(red) Then
                System.IO.File.Delete(red)
            ElseIf Dir(red) <> "" Then
                MsgBox("Unable to delete" & red, MsgBoxStyle.Critical)
            Else
   CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItem(0))        MsgBox(String.Format("File {0} successfully deleted", red), MsgBoxStyle.Information)
            End If
        Next
    End Sub



This still only does what the first example does and truly I am out of ideas or thoughts of why the file is still not being deleted. Will someone please review this code and possibly give his or her rewritten code to perform the file delete successfully as I have been trying for about a week with more than 3 questions not fully answered.

Thank you for trying to help I appriciate whatever help you give in this issue.....
Posted
Updated 3-Nov-10 20:10pm
v2
Comments
Sunasara Imdadhusen 4-Nov-10 3:25am    
You wan to delete file after use?

As told before, you can't delete items like that in a loop.

http://stackoverflow.com/questions/380451/c-how-do-i-do-i-loop-through-items-in-a-list-box-and-then-remove-that-item[^]

http://generally.wordpress.com/2007/10/24/removing-items-in-a-list-while-iterating-through-it[^]

Also, check the value of red. Is this the actual filename? Check the value of red when setting it:
red = Replace(CheckedListBox1.CheckedItems.Item(i).ToString(), "Threat Found: ","")


Otherwise give more info on the information that is in the listbox. You could add the code that fills it so that can also be checked out.
 
Share this answer
 
Comments
Dale 2012 4-Nov-10 17:55pm    
Ok I have read these two links but as said before I wish to delete the file also and not to just remove the item from the checklistbox. the file I wish to remove is a dummy file.exe that I have made with notepad. The file cant be in use because the file has no real purpose. The name of the .exe is specific to the name Im trying to scan for. Im asking for help with example because I have tried itterating backwards and using an array along with the try catch statement and also using the if,then statement --- system.io.file.exists if yes then system.io.file.delete ---- with no luck in sight.... Maybe one of these methods work but I have written it wrongly please help...
Dale 2012 5-Nov-10 2:05am    
I can fill the code? so it checks out? this all means nothing to me...... Im not sure if this is well known or what but it is frustrating to get answers like this without a example..
E.F. Nijboer 5-Nov-10 14:54pm    
But the most logical reason why the file isn't deleted is because the value of the filename simply doesn't exists.
You might think that the value of red is: "c:\temp\testfile.tmp"
But if the actual value is something like: "c:\temp\testfile.tmp " (-> with extra space at the end) this won't work.
Maybe it's even more obvious, like: "Threat Found : c:\temp\testfile.tmp" (-> the replace doesn't give the expected result.)

Also, if the file is on the root of the drive windows is installed (for most this is c:\testfile.tmp) then you will not succeed anyway because since vista the root is protected.
I would suggest first that the empty try catch block is going to come back to bite you in the backside. If the system experiences problems deleting the file it will throw an exception telling you why, but you are absorbing it in the empty try catch which simply makes it go away.
It could be that the file in question is in use by some other resource on the system. If this is the case, I don't think you'll be able to force delete it like this. Take a look at the exception thrown(if there is one) and use this (MSDN Documentation) to see what is happening.
 
Share this answer
 
Comments
Dale 2012 4-Nov-10 17:44pm    
what would you do in this case? can you give some examples or modify the code I have above to work?

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