There's a real problem with that code. You're reading the whole file twice. There's not much point reading an entire file to find out how many lines are in it and thowing away the very data you want to store. What if the file was really big? You basically have three options to read a file like that.
1. Read the entire file into a single string and then split that string into lines:
VB Code:
Dim fileContents As String = myStreamReader.ReadToEnd()
Dim lines As String() = System.Text.RegularExpressions.Regex.Split(fileContents, Environment.NewLine)
2. Use a collection instead of an array. You can copy the data from the collection to an array when you're don if you really need an array:
VB Code:
Dim fileContents As New Specialized.StringCollection
Do Until myStreamReader.Peek() = -1
fileContents.Add(myStreamReader.ReadLine())
Next
Dim lines(fileContents.Count - 1) As String
fileContents.CopyTo(lines, 0)
3. Use an array and resize it as required. The resizing should be done in blocks rather than one element at a time. This is the exact method that simple collections use:
VB Code:
Dim lines(16 - 1) As String 'Start with a capacity of 16.
Dim lineCount As Integer = 0
Do Until myStreamReader.Peek() = -1
If lineCount = lines.Length Then
'The array is full so double its capacity.
ReDim Preserve lines(2 * lineCount - 1)
End If
'Read the next line.
lines(lineCount) = myStreamReader.ReadLine()
lineCount += 1
Loop
'Remove unused elements if desired.
ReDim Preserve lines(lineCount - 1)
Option 1 is preferable as the code is simple but gets less desirable as the file size gets large as reading the file as a block uses more memory. Option 2 is the best general purpose choice. Option 3 is the most efficient but also requires the most complex code.