Results 1 to 8 of 8

Thread: Cast from type 'FileInfo' to type 'String' is not valid. *[resolved]*

  1. #1

    Thread Starter
    Addicted Member ProgrammerJon's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    203

    Cast from type 'FileInfo' to type 'String' is not valid. *[resolved]*

    Code:
            ' make a reference to a directory
            Dim di As New IO.DirectoryInfo("c:\program files\jp\news\data\")
            Dim diar1 As IO.FileInfo() = di.GetFiles("*.newspaper")
            Dim dra As IO.FileInfo
            'list the names of all files in the specified directory
            For Each dra In diar1
                ListBox1.Items.Add(dra)
            Next

    Code:
    Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
    Dim filename As String = ListBox1.SelectedItem ' this dos not work
    End Sub

    When I dubble click listbox1 then I get this error:

    ***************************************************
    An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll

    Additional information: Cast from type 'FileInfo' to type 'String' is not valid.
    ***************************************************

    I need to be able to add the selected text into a FileOpen command:



    Code:
    FileOpen(1, "C:/program files/jp/news/data/" & filename, OpenMode.Input)
    I there another way I can do this?
    Last edited by ProgrammerJon; Feb 9th, 2003 at 11:43 AM.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    VB Code:
    1. Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
    2. Dim filename As String = CStr(ListBox1.SelectedItem)
    3. End Sub

  3. #3

    Thread Starter
    Addicted Member ProgrammerJon's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    203
    I am still getting the same error.
    I think that I might do some more debuging and check if the problem is somewere else.

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Post the whole code and I will see if I can find the error for you.

  5. #5

    Thread Starter
    Addicted Member ProgrammerJon's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    203

    Program

    Ok I attched the program. Not much is working on it becuase I got stuck on the problem early.

    Look at the readme.txt file.
    Look at the Create.bmp file

    Thanks for helping!




    Edit: You might want to change the paths of the FileOpen and filesearch because it is set to "C:/program files/jp/news/data"
    or you could just create that directory(the program won't do it for you)
    Attached Files Attached Files

  6. #6
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Ok, I got it figured out for you.

    I had to change all your hard coded paths of "C:/program files/jp/news/data/" to Application.StartupPath....which is something I suggest you go ahead and use yourself that way nothing is hard coded.

    I marked the two areas that I fixed with what I believe to be the problem. No more errors when I run the app.
    Attached Files Attached Files

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    To make it easier on you so you don't have to download the whole file, here is what I changed:

    VB Code:
    1. Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
    2.         Dim address As String
    3.         Dim method1 As String
    4.         'This part dosn't work;
    5.  
    6.         '****************I changed this:*************************************
    7.         ' The reason why it didn't work is because the object you stored in the listbox
    8.         ' is a fileinfo object.  You need to create a file info object to get the selected
    9.         ' item out, then get the actual name of the fileinfo object because that is what
    10.         ' your after.
    11.         Dim fi As IO.FileInfo = ListBox1.SelectedItem
    12.         Dim filename As String = fi.Name.ToString()
    13.         '****************End change******************************************
    14.  
    15.         ' You will want to change the path or else create these directories C:/program files/jp/news/data/
    16.         ' If I change the variable filename in the path to listbox1.selecteditem then it dosn't work.
    17.         FileOpen(1, Application.StartupPath & "\" & filename, OpenMode.Input)
    18.         'I have it setup so that the name of the file is the persons name, the first line is the persons address then followed by a comma, then I have the collection method
    19.         Input(1, address)
    20.         Input(1, method1)
    21.  
    22.         '**************I changed this:******************************
    23.         ' Again, you are trying to use a fileinfo object as a string.  I switched
    24.         ' it so it works like I think you wanted, you may have to adjust it.
    25.         ' Just getting the name without an extension on it.
    26.         Dim name As String = IO.Path.GetFileNameWithoutExtension(filename)
    27.         TextBox1.Text = name 'Put the name in Textbox1
    28.         '*****************End change*********************************
    29.  
    30.         TextBox2.Text = address 'Put the address in Textbox2
    31.         Select Case method1 'Decide what RadioButton to check
    32.             Case Is = "normal"
    33.                 RadioButton1.Checked = True
    34.             Case Is = "gold"
    35.                 RadioButton2.Checked = True
    36.             Case Else
    37.                 RadioButton3.Checked = True
    38.         End Select
    39.  
    40.     End Sub

  8. #8

    Thread Starter
    Addicted Member ProgrammerJon's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    203
    Thanks, I works good now!

Posting Permissions

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



Click Here to Expand Forum to Full Width