-1

Im trying to output the data from the second line of my textfile to a datagridview but when doing so it is also outputting every line after the the second line. This is what I have tried. Thanks

        Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
        For index = 1 To lines.Length - 1
            Dim cells = lines(index).Split(","c)
            dgvOutput.Rows.Add(cells)
            FileClose()
Padraig
  • 1
  • 1
  • where is the `Next`? please provide a minimum, complete, verifiable example. – FalcoGer Mar 24 '19 at 20:01
  • Please could you explain what is different in this question from your previous one. Do you want to read all the lines starting from the second one or just the second line? – Steve Mar 24 '19 at 20:13

3 Answers3

1

Your loop is executed over all lines skipping just the first line.
While I cannot see what happen in the FileClose call it seems to not have any sense because ReadAllLines has already closed the file.

You can get the second line of your file with a single line of code

Dim line as String = File.ReadLines(OrderID & ".txt").Skip(1).Take(1).FirstOrDefault()

' this check is required to avoid problems with files containing 0 or 1 line
if line IsNot Nothing Then
    Dim cells = line.Split(","c)
    dgvOutput.Rows.Add(cells)
End If

Notice that I have replaced the ReadAllLines with ReadLines. This is better because using this method you don't read all lines when you need only the second one (if it exists). More info at ReadLines vs ReadAllLines

Steve
  • 213,761
  • 22
  • 232
  • 286
  • One small point: as you're calling `Take(1)` then there can never be more than one line, so `SingleOrDefault` would be more appropriate than `FirstOrDefault`. – jmcilhinney Mar 24 '19 at 21:35
1

It's outputting every line after the second line, because that's what you're telling it to do when you iterate through the array of strings returns from ReadAllLines.

IO.File.ReadAllLines does not leave an output stream open. The file is closed. What it does do, is return a zero-based (by default) array of the contents of the file, with line breaks being the delimiter for the split.

To just get the contents of the second line, using ReadAllLines, this is what you need:

Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
If lines.length >= 2 Then 
   Dim cells = lines(1).Split(","c)
   dgvOutput.Rows.Add(cells) 
End If 

Now, that does have the overhead of reading the entire file in. If you open the file using a reader object, then you only need to read the first and second lines of the file to get that second line.

That would be something like this:

Dim reader as StreamReader = My.Computer.FileSystem.OpenTextFileReader(OrderId & ".txt")
Dim a as String
' This reads the first line, which we throw away 
reader.ReadLine()
a = reader.ReadLine()
reader.Close()
Dim cells = a.Split(","c)
dgvOutput.Rows.Add(cells) 

You would need to test your explicit circumstances to determine which is better for what you're trying to do.

Stephen Wrighton
  • 36,783
  • 6
  • 67
  • 86
0
Dim lines = IO.File.ReadAllLines(OrderID & ".txt")
Dim SecondLine = lines(1)

File.ReadAllLines opens and closes the file for you so there is not need to add code to close it.

Mary
  • 14,926
  • 3
  • 18
  • 27