I have used a small code snippet which belongs to a simple checklistbox file delete program. The original program works with the code but my program does not function the same and returns an exception as follows:
'The given path's format is not supported'.
the line of code that the exception happens to is:
Dim fileInfo As New FileInfo(item.ToString())
The source code Im trying to apply to my program is as follows:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each foundFile As String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.Temp, FileIO.SearchOption.SearchAllSubDirectories, "*.*") CheckedListBox1.Items.Add(foundFile)
Next
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
For Each item As Object In CheckedListBox1.CheckedItems
If item.GetType() Is GetType(FileInfo) Then
CType(item, FileInfo).Delete()
ElseIf item.GetType() Is GetType(String) Then
Dim fileInfo As New FileInfo(item.ToString())
On Error Resume Next
fileInfo.Delete()
End If
Next
While CheckedListBox1.CheckedItems.Count > 0
CheckedListBox1.Items.Remove(CheckedListBox1.CheckedItems(0))
End While
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
For i As Integer = 0 To CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(i, True)
Next
Else
For i As Integer = 0 To CheckedListBox1.Items.Count - 1
CheckedListBox1.SetItemChecked(i, False)
Next
End If
End Sub
End Class
The problem with this code is that I need the button3 click event (delete) to delete the file that is associated with the item found and not to load the file system special directories "*.*" My program is a scanner so it needs to be able to delete and remember the paths of the names it finds in my.resources.viruslist. Can anyone write this code to properly delete files that are found in the checklistbox items area?