Private Sub ProcessFile(ByVal strFile As String) 'Declare an object for the StreamReader class Dim objReader As StreamReader Try 'Open the file objReader = File.OpenText(strFile) Catch e As System.Exception 'Display the messages MessageBox.Show(e.ToString) MessageBox.Show(e.Message) 'Exit the procedure Exit Sub End Try 'Declare variables Dim intLineCount As Integer = 0 Dim strLine As String, strData As String 'Loop through the file counting the lines and loading the Do Try 'Read a line from the file strLine = objReader.ReadLine intLineCount = intLineCount + 1 Catch e As EndOfStreamException 'Display the message MessageBox.Show(e.Message) 'Exit the Do loop Exit Do Finally 'Concatenate the data to the strData variable strData = strData & strLine & vbCrLf End Try Loop While strLine <> Nothing 'Load the text box txtData.Text = strData 'Display the number of lines read lblLinesOfText.Text = "Number of lines read: " & intLineCount - 1 'Clean up objReader.Close() objReader = Nothing End Sub
MessageBox.Show(System.IO.File.ReadAllLines(@"D:\T.txt").Count().ToString());