清单框文件删除 [英] Checklistbox File Delete
问题描述
我之前的问题没有得到正确回答,但是我一直在寻找正确的答案,但运气不好.当我的扫描仪找到我要删除的文件时,我的代码不会删除复选框列表项.该项目已从复选框中删除,但实际文件仍保留在计算机中,这一开始就破坏了我的扫描目的.
删除代码:
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.
推荐答案
在调用System.IO.File.Delete(red)
之前,请先调用System.IO.File.Exists(red)
以确保文件路径有效.仅在Exists
返回真
时调用Delete
,否则显示错误消息框,以使您知道未获得正确的文件路径.
Before callingSystem.IO.File.Delete(red)
, callSystem.IO.File.Exists(red)
to make sure the file path is valid. Only callDelete
ifExists
returnsTrue
, otherwise show an error message box, so you know that you did not get the proper file path.
这篇关于清单框文件删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!