|
-
Aug 15th, 2012, 07:31 PM
#1
Thread Starter
Junior Member
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!
-
Aug 15th, 2012, 07:34 PM
#2
Thread Starter
Junior Member
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
-
Aug 15th, 2012, 08:00 PM
#3
Lively Member
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
-
Aug 15th, 2012, 10:01 PM
#4
Lively Member
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.
-
Aug 16th, 2012, 02:16 AM
#5
Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.
You must have added Strings in ListBox2, not FileInfo objects.
-
Aug 16th, 2012, 03:08 AM
#6
Thread Starter
Junior Member
Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.
Thanks for the replies!
 Originally Posted by elielCT
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)
-
Aug 16th, 2012, 03:48 AM
#7
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:
ListBox2.Items.addrange(io.file.readalllines(My.Application.Info.DirectoryPath & "\mods.cfg"))
to rewrite your file:
vb.net 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
End Using
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 16th, 2012, 04:41 AM
#8
Thread Starter
Junior Member
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
-
Aug 16th, 2012, 04:57 AM
#9
Thread Starter
Junior Member
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.
-
Aug 16th, 2012, 05:02 AM
#10
Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.
ListBox2.Items.Add(ListBox1.SelectedItem)
ListBox2, until that point, contains strings. ListBox1 contains IO.FileInfos.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 16th, 2012, 05:13 AM
#11
Thread Starter
Junior Member
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?
-
Aug 16th, 2012, 05:22 AM
#12
Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.
instead of this:
vb.net Code:
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
you can use this to display full filepaths:
vb.net Code:
ListBox1.Items.AddRange(IO.Directory.GetFiles(My.Application.Info.DirectoryPath, "*.mod"))
or this for filenames with extensions:
vb.net Code:
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:
ListBox1.Items.AddRange(IO.Directory.GetFiles(My.Application.Info.DirectoryPath, "*.mod").Select(Function(s) IO.Path.GetFileNameWithoutExtension(s)).ToArray)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 16th, 2012, 05:25 AM
#13
Thread Starter
Junior Member
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
-
Aug 18th, 2012, 01:14 AM
#14
Lively Member
Re: Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|