File Open

|
OpenFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
'OpenFileDialog1.InitialDirectory = "C:Program Files"

OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files(*.*)|*.*"

OpenFileDialog1.FileName = ""

OpenFileDialog1.FilterIndex = 1

'Check to see if the user clicked the open button
If (OpenFileDialog1.ShowDialog() = DialogResult.OK) Then
TextBox1.Text = OpenFileDialog1.FileName

Try
Using sr As New StreamReader(TextBox1.Text)
Dim line As String
line = sr.ReadToEnd()
TextBox2.Text = line
End Using
Catch ex As Exception
MessageBox.Show("The file could not be read:" & Environment.NewLine & ex.Message.ToString())
End Try

Else
'MsgBox("You did not select a file!")
End If
Originally Posted on March 11, 2013
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.