7 Replies - 2731 Views - Last Post: 12 March 2011 - 08:37 AM Rate Topic: -----

#1 MIKE2098  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-May 08

Trying to load text file of mp3 file paths to listbox

Posted 07 March 2011 - 12:03 PM

Hi I am trying to load a text file into a list box in order to be able to play the files see code below I have an error on this line

Which states Error 1 Value of type 'String' cannot be converted to '1-dimensional array of String'.


        Dim Open As New OpenFileDialog
        Dim myStreamReader As System.IO.StreamReader
        Open.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
        Open.CheckFileExists = True
        Open.Title = "OpenFile"
        Open.ShowDialog(Me)
        Try


            If (Open IsNot Nothing) Then
                ' Insert code to read the stream here.
                Dim filepath() As String = myStreamReader.ReadToEnd()
                Dim ffile As String
                For Each ffile In filepath
                    Me.ListBoxFileName.Items.Add(System.IO.Path.GetFileNameWithoutExtension(ffile))
                    Me.ListBoxFilePath.Items.Add(System.IO.Path.GetFullPath(ffile))
                Next

            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally
            ' Check this again, since we need to make sure we didn't throw an exception on open.
            If (Open IsNot Nothing) Then
                myStreamReader.Close()
            End If
        End Try
        End


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to load text file of mp3 file paths to listbox

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Trying to load text file of mp3 file paths to listbox

Posted 07 March 2011 - 12:17 PM

myStreamReader.ReadToEnd() returns a string, not a string array.
If every line of your file ends with CrLf(Enter), use Split function.
Dim filepath() As String = myStreamReader.ReadToEnd().Split(vbCrLf);


This is a quick example and i haven't test it
Was This Post Helpful? 0
  • +
  • -

#3 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Re: Trying to load text file of mp3 file paths to listbox

Posted 07 March 2011 - 12:20 PM

This should work:

Dim Open As New OpenFileDialog
Dim myStreamReader As System.IO.StreamReader
Open.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
Open.CheckFileExists = True
Open.Title = "OpenFile"
Open.ShowDialog(Me)
Try


    If (Open IsNot Nothing) Then
        ' Insert code to read the stream here.
        Dim filepath As String = myStreamReader.ReadToEnd()
        Dim ffile As String
        For Each ffile In filepath
            Me.ListBoxFileName.Items.Add(System.IO.Path.GetFileNameWithoutExtension(ffile))
            Me.ListBoxFilePath.Items.Add(System.IO.Path.GetFullPath(ffile))
        Next

    End If
Catch Ex As Exception
    MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
    ' Check this again, since we need to make sure we didn't throw an exception on open.
    If (Open IsNot Nothing) Then
        myStreamReader.Close()
    End If
End Try
End




I'm pretty sure you were getting the error because when declaring "Filepath" you put () at the end of filepath which will make VB think you're trying to declare an array of strings rather than just one string.

This post has been edited by Jack Eagles1: 07 March 2011 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

#4 MIKE2098  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-May 08

Re: Trying to load text file of mp3 file paths to listbox

Posted 09 March 2011 - 01:04 AM

View PostJack Eagles1, on 07 March 2011 - 12:20 PM, said:

This should work:

Dim Open As New OpenFileDialog
Dim myStreamReader As System.IO.StreamReader
Open.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
Open.CheckFileExists = True
Open.Title = "OpenFile"
Open.ShowDialog(Me)
Try


    If (Open IsNot Nothing) Then
        ' Insert code to read the stream here.
        Dim filepath As String = myStreamReader.ReadToEnd()
        Dim ffile As String
        For Each ffile In filepath
            Me.ListBoxFileName.Items.Add(System.IO.Path.GetFileNameWithoutExtension(ffile))
            Me.ListBoxFilePath.Items.Add(System.IO.Path.GetFullPath(ffile))
        Next

    End If
Catch Ex As Exception
    MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
Finally
    ' Check this again, since we need to make sure we didn't throw an exception on open.
    If (Open IsNot Nothing) Then
        myStreamReader.Close()
    End If
End Try
End




I'm pretty sure you were getting the error because when declaring "Filepath" you put () at the end of filepath which will make VB think you're trying to declare an array of strings rather than just one string.


Hi thanks for the reply I get an error when I try to run your code object referance not set to an instance of an object, the text file I want to read contains

C:\Users\Public\Music\Sample Music\Sleep Away.mp3
C:\Users\Public\Music\Sample Music\Maid with the Flaxen Hair.mp3
C:\Users\Public\Music\Sample Music\Kalimba.mp3
Was This Post Helpful? 0
  • +
  • -

#5 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Trying to load text file of mp3 file paths to listbox

Posted 09 March 2011 - 02:42 AM

You got the error because you haven't initialized the myStreamReader object, you just declared it.
If (Open IsNot Nothing) Then  
    ' Insert code to read the stream here.  so this line here is to avoid that error
   Dim filepath As String = myStreamReader.ReadToEnd()  


Remove the initial declaration and add this instead of the comment
Dim myStreamReader As StreamReader = New StreamReader(open.Filename)


This post has been edited by Ionut: 09 March 2011 - 02:43 AM

Was This Post Helpful? 1
  • +
  • -

#6 MIKE2098  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-May 08

Re: Trying to load text file of mp3 file paths to listbox

Posted 10 March 2011 - 12:58 AM

View PostIonut, on 09 March 2011 - 02:42 AM, said:

You got the error because you haven't initialized the myStreamReader object, you just declared it.
If (Open IsNot Nothing) Then  
    ' Insert code to read the stream here.  so this line here is to avoid that error
   Dim filepath As String = myStreamReader.ReadToEnd()  


Remove the initial declaration and add this instead of the comment
Dim myStreamReader As StreamReader = New StreamReader(open.Filename)



Thanks for the reply my code is now

   Dim Open As New OpenFileDialog
        Dim myStreamReader As System.IO.StreamReader
        Open.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
        Open.CheckFileExists = True
        Open.Title = "OpenFile"
        Open.ShowDialog(Me)
        Try


            If (Open IsNot Nothing) Then
                Dim myStreamReader As StreamReader = New StreamReader(Open.FileName)

                Dim filepath As String = myStreamReader.ReadToEnd()
                Dim ffile As String
                For Each ffile In filepath
                    Me.ListBoxFileName.Items.Add(System.IO.Path.GetFileNameWithoutExtension(ffile))
                    Me.ListBoxFilePath.Items.Add(System.IO.Path.GetFullPath(ffile))
                Next

            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally
            ' Check this again, since we need to make sure we didn't throw an exception on open.
            If (Open IsNot Nothing) Then
                myStreamReader.Close()
            End If
        End Try
        End


I now recive the following error "Variable 'myStreamReader' hides a variable in an enclosing block."

So I removed the previous referance

Private Sub RadMenuItemLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadMenuItemLoad.Click
        Dim Open As New OpenFileDialog
        ' Dim fStreamReader As System.IO.StreamReader
        Open.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
        Open.CheckFileExists = True
        Open.Title = "OpenFile"
        Open.ShowDialog(Me)
        Try


            If (Open IsNot Nothing) Then
                Dim myStreamReader As StreamReader = New StreamReader(Open.FileName)
                Dim filepath As String = myStreamReader.ReadToEnd()
                Dim ffile As String
                For Each ffile In filepath
                    Me.ListBoxFileName.Items.Add(System.IO.Path.GetFileNameWithoutExtension(ffile))
                    Me.ListBoxFilePath.Items.Add(System.IO.Path.GetFullPath(ffile))
                Next

            End If
        Catch Ex As Exception
            MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
        Finally
            ' Check this again, since we need to make sure we didn't throw an exception on open.
            If (Open IsNot Nothing) Then
                '  myStreamReader.Close()
            End If
        End Try
        End




    End Sub
End Class


I can see filepath gets the following string

C:\Users\Public\Music\Sample Music\Sleep Away.mp3" & vbCrLf & "C:\Users\Public\Music\Sample Music\Maid with the Flaxen Hair.mp3" & vbCrLf & "C:\Users\Public\Music\Sample Music\Kalimba.mp3" & vbCrLf & ""

So I think we are close the following lines are designed to read file paths when a user selects files from a directory

Me.ListBoxFileName.Items.Add(System.IO.Path.GetFileNameWithoutExtension(ffile))
Me.ListBoxFilePath.Items.Add(System.IO.Path.GetFullPath(ffile))

I tried the split string method mentioned above but that did not work when it goes through the file names in ffile it is loading each char i.e. C then : then \ so its not using the full string and then gives an exception
Was This Post Helpful? 0
  • +
  • -

#7 MIKE2098  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-May 08

Re: Trying to load text file of mp3 file paths to listbox

Posted 10 March 2011 - 01:10 AM

I have added
For Each ffile In filepath.Split(vbCrLf)
and it now shows the full path I now get an error Illegal characters in path. yet the path names are

C:\Users\Public\Music\Sample Music\Sleep Away.mp3
C:\Users\Public\Music\Sample Music\Maid with the Flaxen Hair.mp3

If I can use ffile as the filepath and then need some way to split the filename by removing the folder info and file ext which is what I thought GetFileNameWithoutExtension(ffile)) would do
Was This Post Helpful? 0
  • +
  • -

#8 MIKE2098  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 05-May 08

Re: Trying to load text file of mp3 file paths to listbox

Posted 12 March 2011 - 08:37 AM

Can anyone help me work this out?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1