Hi i am trying to delete a file(s) from a checked list box, when i click a button. This is the code i have:

Dim item As IO.FileInfo
        For Each item In CheckedListBox1.CheckedItems
            Try
                item.Delete()
            Catch : End Try
        Next item

But i get the error: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

Am i doing something really stupid lol, any help would be appreciated

Many thanks

Bagleys

Did you store the item in the CheckedListBox as a string or as a FileInfo? It seems as if it's a string. If so, there are 2 quick options. Rewrite the code that put the objects in the list so that they are added as FileInfo objects, or simply establish a new FileInfo object using the string from the collection.

Consider the following code (forgive the syntax, I work more easily in C#, but I've also included a VB version)

private void Form1_Load(object sender, EventArgs e)
        {
            checkedListBox1.Items.Add(@"C:\Work\test1.txt");
            checkedListBox1.Items.Add(@"C:\Work\test2.txt");
            FileInfo fileInfo = new FileInfo(@"C:\Work\test3.txt");
            checkedListBox1.Items.Add(fileInfo);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (object item in checkedListBox1.CheckedItems)
            {
                if (item.GetType() == typeof(FileInfo))
                {
                    ((FileInfo)item).Delete();
                }
                else if (item.GetType() == typeof(string))
                {
                    FileInfo fileInfo = new FileInfo(item.ToString());
                    fileInfo.Delete();
                }
            }
        }
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        CheckedListBox1.Items.Add("C:\Work\test1.txt")
        CheckedListBox1.Items.Add("C:\Work\test2.txt")
        Dim fileInfo As New FileInfo("C:\Work\test3.txt")
        CheckedListBox1.Items.Add(fileInfo)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.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())
                fileInfo.Delete()
            End If

        Next

    End Sub

I've added two strings and one FileInfo object to the CheckedBoxList during Form1_Load. On the button click event, I've iterated through the checked items and tested the type of each object, and handled the item accordingly.

Hi thanks for your reply, im at col at the moment so will try it later.

Many thanks

Bagleys

Hi

Try this

Private Sub Command1_Click()
With Me.List1
.AddItem "a"
.AddItem "b"
.AddItem "c"
.AddItem "1"
.AddItem "2"
.AddItem "3"
End With
End Sub

Private Sub Command2_Click()
Dim i As Integer

'START AT LAST LIST ITEM AND WORK TO FIRST
For i = Me.List1.ListCount - 1 To 0 Step -1
'IS SELECTED?
If Me.List1.Selected(i) = True Then
'REMOVE SELECTED ITEMS
Me.List1.RemoveItem (i)
End If
Next
End Sub

Hi thanks i have tried the code, the error code i was getting has gone now but the files don't delete, any ideas?

Here's the code iv got:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim folderInfo As New IO.DirectoryInfo("C:\Documents and Settings\Andy\Application Data\uTorrent")
        Dim arrFilesInFolder() As IO.FileInfo
        Dim fileInFolder As IO.FileInfo
        arrFilesInFolder = folderInfo.GetFiles("*.torrent")
        For Each fileInFolder In arrFilesInFolder
            CheckedListBox1.Items.Add(fileInFolder.Name)
        Next
    End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.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())
                    fileInfo.Delete()
                End If
            Next

        End If
    End Sub

You want to change fileInFolder.Name to fileInFolder.FullName.

.Name will only store the actual name of the file, whereas .FullName will give you the path and name, which is what you'll need when you instantiate a FileInfo object on the button click event in order to delete it.

Alternately, you can just use CheckedListBox1.Items.Add(fileInFolder) and skip the whole naming issue altogether. In that case, the FileInfo object itself will be in the list, and the checked items will be handled accordingly by the button click event.

Lastly, if you do not want to see the full path in the CheckedListBox, use fileInFolder.Name but reference the path when establishing a new instance in the button click event.

Meaning, change code to the following:

Dim filePath as String
                filePath = "C:\yourpathhere\"
                Dim fileInfo As New FileInfo(filePath & item.ToString())
                fileInfo.Delete()

Thank you so much for your help its working now. I have put in a new button to delete the files and it works great :). The button i was using is performing lots of other functions as well, so for some reason didn't delete the items; I think i need to restructure the events within the button1.

Thanks again for your help, much appreciated

Bagleys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.