vb Code:
'
Public Class Form1
Private sr As IO.StreamReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Debug.Assert(Not IsNothing(sr))
' checking whether we're not at the end yet
If Not (sr.EndOfStream) Then
' Read the next line and display in in the texbox
TextBox1.Text = sr.ReadLine()
Else
MessageBox.Show("Reached the end of file")
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
sr.Close()
sr.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using dlg As New OpenFileDialog With {.CheckFileExists = True}
Dim ret As DialogResult = dlg.ShowDialog(Me)
If ret = Windows.Forms.DialogResult.OK Then
Try
' Open file for reading
sr = New IO.StreamReader(New IO.FileStream(dlg.FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read))
Catch ex As Exception
' Catching possible exceptions
MessageBox.Show(ex.Message)
End Try
End If
End Using
End Sub
End Class