Search code examples
vb.netreadfile

Unable to read multiple lines in text file vb.net


Im not sure what I'm doing wrong I am trying to read a text file containing a number of file paths with the file name and extension. I have managed split the file path and file name to add to my dictionary which is then added to a listbox.

The problem is my code is repeatedly reading the same line. Can any one give advice on where I am going wrong. The text file is formatted as...

C:\Users\User\Desktop\Music track3@01. Aerosmith & Run DMC - Walk This Way (1986).mp3 C:\Users\User\Desktop\Music track3@01. INXS - I Need You Tonight (1988).mp3

Im using the @ char to help with separating the file path and name.

    Dim lines = File.ReadAllLines("tmp.txt")
        'read from a file
        Dim line As String
        Using reader As New StreamReader("tmp.txt")
            line = reader.ReadLine()
            Dim str = line
            Dim split = str.Split("@")
            Dim FilePathValue As String
            Dim FileNameKey As String

            If (split.Count = 2) Then
                FilePathValue = split(0).ToString()
                FileNameKey = split(1).ToString()
                '
            End If
            ' Load items into the music dictionary
            For i = 0 To lines.Length - 1 Step 2
                MsgBox("Path should not be blank??? = " & 
    FilePathValue & " File Name = " & FileNameKey)
                MusicDictionary.Add(FileNameKey(i), FilePathValue(i))
                UpdateTheDictionary()
            Next

            'Here we need to load the filename into the dropdown items
            For Each item In MusicDictionary
                'Dim x = 
    MusicToolStripMenuItem.DropDownItems.Add(item.Key)
                'AddHandler x.Click, AddressOf ToolMenuItem_Click
            Next

        End Using

Thanks


Solution

  • You read your file twice:

    You read the file into an array

    Dim lines = File.ReadAllLines("tmp.txt")
    

    Then you also start reading it again, this time in a "line by line" way, but you only read the first line:

    Using reader As New StreamReader("tmp.txt")
    line = reader.ReadLine()
    

    You process this first line, pull the filename out etc, and then you repeatedly act on this same line, by looping over the array you read:

    For i = 0 To lines.Length - 1 Step 2
    

    Hopefully you can understand that your first instruction read all the lines of the file read them into an array in one part of memory, and then reading the first line again put another variable in a different part of the memory, and there is absolutely no link at all between these two items of data. Looping over the array will not change the contents of the line variable (nor would it carry out the split again etc)


    What to do about it?

    Remove the existing for loops, and replace the Using with a For Each line in lines, and debug the result. To debug, place the cursor on the first line of the code, press F9 so it goes red, and run the program. When everything stops with a yellow marker on the first line you can use the Step Over/Into/Out commands on the debug menu to go through the code (use their key shortcuts, much easier)