Results 1 to 14 of 14

Thread: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

  1. #1
    New Member
    Join Date
    Aug 12
    Posts
    15

    Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    Hey everyone,

    Code:
            Using SW As New IO.StreamWriter(My.Application.Info.DirectoryPath & "\mods.cfg", True)
                For Each itm As FileInfo In Me.ListBox2.Items  <--- error points to this line
                    SW.WriteLine(itm)
                Next
    Using that code, i get the exception: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    The idea behind the code is swapping items between two text boxes, this bit is called to update a text file which stores the remaining items after an item is deleted from the list.
    This code works fine if i start with a blank list, i can add and remove items from the list with no problems.
    If i exit the program and reload with items stored in the second list, and i remove from the list i get that problem, but only if theres more than 1 item in the list. If its just the 1 item it works fine.

    The code im using to add to the list is almost the same,

    Code:
    For Each itm As FileInfo In Me.ListBox2.Items
                If itm.Name = ListBox1.Text Then
                    MsgBox("Already Active!", , "title")
                    Exit Sub
                End If
            Next
    
            ListBox2.Items.Add(ListBox1.SelectedItem)
    
            Using SW As New IO.StreamWriter(My.Application.Info.DirectoryPath & "\mods.cfg", True)
    
                SW.WriteLine(ListBox1.Text)
    
            End Using
    And i can add fine using the code, so i'm stumped as to where i've gone wrong.

    Thanks for any help in advance!

  2. #2
    New Member
    Join Date
    Aug 12
    Posts
    15

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    The idea behind the code is swapping items between two text boxes
    Doesnt let me edit yet, meant to say listboxes

  3. #3
    Lively Member elielCT's Avatar
    Join Date
    Jun 11
    Posts
    77

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    Code:
    Using SW As New IO.StreamWriter(My.Application.Info.DirectoryPath & "\mods.cfg", True)
                For Each itm As  string  In Me.ListBox2.Items                  
                         SW.WriteLine(itm)
                Next
    SW.dispose()
    Last edited by elielCT; Aug 15th, 2012 at 09:53 PM. Reason: misread code

  4. #4
    Lively Member elielCT's Avatar
    Join Date
    Jun 11
    Posts
    77

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    Change FileInfo to string in your other procedure too..

    Items in a listbox aren't of type FileInfo.

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 11
    Posts
    3,143

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    You must have added Strings in ListBox2, not FileInfo objects.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | Create Sortable BindingList(not mine) | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading


    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. -jmcilhinney

  6. #6
    New Member
    Join Date
    Aug 12
    Posts
    15

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    Thanks for the replies!

    Quote Originally Posted by elielCT View Post
    Change FileInfo to string in your other procedure too..

    Items in a listbox aren't of type FileInfo.
    Using that i get "Conversion from type 'FileInfo' to type 'String' is not valid."


    You must have added Strings in ListBox2, not FileInfo objects.
    How would i force that?
    Im inputting to list box 2 from a text file using:

    Code:
           ListBox2.Items.Clear()
            FileOpen(1, My.Application.Info.DirectoryPath & "\mods.cfg", OpenMode.Input)
    
    
    
            Do While Not EOF(1)
                Input(1, ModName)
                ListBox2.Items.Add(ModName)
            Loop
    
            FileClose(1)

  7. #7
    vb Coda .paul.'s Avatar
    Join Date
    May 07
    Location
    Chelmsford UK
    Posts
    16,508

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    the method you're using in your first post is vb6 legacy code. there are better ways to load your listbox:

    vb.net Code:
    1. ListBox2.Items.addrange(io.file.readalllines(My.Application.Info.DirectoryPath & "\mods.cfg"))

    to rewrite your file:

    vb.net Code:
    1. Using SW As New IO.StreamWriter(My.Application.Info.DirectoryPath & "\mods.cfg", True)
    2.    For Each itm As String In Me.ListBox2.Items
    3.        SW.WriteLine(itm)
    4.    Next
    5. End Using

  8. #8
    New Member
    Join Date
    Aug 12
    Posts
    15

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    Thanks, the help is much appreciated!

    This is my complete(ish) code

    Code:
        Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
            'refreshes list from file
            ListBox2.Items.Clear()
    
    
            ListBox2.Items.AddRange(IO.File.ReadAllLines(My.Application.Info.DirectoryPath & "\mods.cfg"))
    
            ListBox1.Items.Clear()
    
            Dim ModDir As New IO.DirectoryInfo(My.Application.Info.DirectoryPath)
            For Each myfile As IO.FileInfo In ModDir.GetFiles("*.mod", IO.SearchOption.TopDirectoryOnly)
                ListBox1.Items.Add(myfile)
            Next
    
        End Sub
    
        Private Sub cmd_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Add.Click
    
              For Each itm As String In Me.ListBox2.Items   ****ERRORS HERE*****
              If itm = ListBox1.Text Then
                  MsgBox("Already Active!", , "Mod Manager")
                 Exit Sub
               End If
             Next
    
            ListBox2.Items.Add(ListBox1.SelectedItem)
    
            Using SW As New IO.StreamWriter(My.Application.Info.DirectoryPath & "\mods.cfg", True)
    
                SW.WriteLine(ListBox1.Text)
    
            End Using
        End Sub
    
    
    
        Private Sub cmd_Remove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_Remove.Click
    
            ListBox2.Items.Remove(ListBox2.SelectedItem)
    
            Using SW As New IO.StreamWriter(My.Application.Info.DirectoryPath & "\mods.cfg", True)
                For Each itm As String In Me.ListBox2.Items   ***ERRORS HERE****
                    SW.WriteLine(itm)
                Next
                SW.Dispose()
    
            End Using
    
        End Sub
    Now i have "Conversion from type 'FileInfo' to type 'String' is not valid." at the two places I've marked. One is when adding to the second list, and the second one is when removing an item from the list.
    I guess the items are being added to the listbox as fileinfo types or something, im not sure

  9. #9
    New Member
    Join Date
    Aug 12
    Posts
    15

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    Ok i've narrowed it down to

    - Remove function will work perfectly if used first
    - As soon as i click add to list, it will add the item i've selected, but first time only
    - Any subsequent used of either add or remove will result in a crash.

  10. #10

  11. #11
    New Member
    Join Date
    Aug 12
    Posts
    15

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    Ahh ok! You can tell the last time i did VB it was in vb6..

    Code:
      For Each myfile As IO.FileInfo In ModDir.GetFiles("*.mod", IO.SearchOption.TopDirectoryOnly)
    I think thats the guilty line for making listbox1 fileinfo?

    If so, how would i go about changing that to strings?

  12. #12
    vb Coda .paul.'s Avatar
    Join Date
    May 07
    Location
    Chelmsford UK
    Posts
    16,508

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    instead of this:

    vb.net Code:
    1. Dim ModDir As New IO.DirectoryInfo(My.Application.Info.DirectoryPath)
    2. For Each myfile As IO.FileInfo In ModDir.GetFiles("*.mod", IO.SearchOption.TopDirectoryOnly)
    3.     ListBox1.Items.Add(myfile)
    4. Next

    you can use this to display full filepaths:

    vb.net Code:
    1. ListBox1.Items.AddRange(IO.Directory.GetFiles(My.Application.Info.DirectoryPath, "*.mod"))

    or this for filenames with extensions:

    vb.net Code:
    1. ListBox1.Items.AddRange(IO.Directory.GetFiles(My.Application.Info.DirectoryPath, "*.mod").Select(Function(s) IO.Path.GetFileName(s)).ToArray)

    or this for filenames without extensions:

    vb.net Code:
    1. ListBox1.Items.AddRange(IO.Directory.GetFiles(My.Application.Info.DirectoryPath, "*.mod").Select(Function(s) IO.Path.GetFileNameWithoutExtension(s)).ToArray)

  13. #13
    New Member
    Join Date
    Aug 12
    Posts
    15

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    You, Sir, are a God!

    The second one worked a charm, i massively appreciate the help

  14. #14
    Lively Member elielCT's Avatar
    Join Date
    Jun 11
    Posts
    77

    Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.

    Glad it got resolved!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •