File Handling: How to Open a Text File in Visual Basic

Submitted by: 
Visitors have accessed this post 3678 times.

In this tutorial I’m going to show you how to to open a text file using Visual Basic. A text file is considered as a plain text and the only capable of creating plain text files and save it to .txt extension is notepad. So to start with this tutorial, open first a visual basic and create a project named “FileHandling”.

Then on the form, add a MenuStrip from a toolbox, and a RichTextBox. Next, we’re going to add a menu to our menu strip such as “File” and under “File”, add “Open” as a sub menu and change the text property to “menuOpen”. And finally,Set the Dock property of RichTextBox into “Fill”. And when you execute this application it should look like as shown below:

At this time, we’re going to add functionality to our application, to do this, double click the “Open” Sub menu. And add the following code:

  1. 'declare open as new openFiledialog so that when the Open Sub menu is click,
  2. 'the OpenfileDialog will appear.
  3. Dim Open As New OpenFileDialog()
  4. 'it is declare as System input and output Streamreader
  5. 'it reads characters from byte stream in a particular encoding
  6. Dim myStreamReader As System.IO.StreamReader
  7. 'in an open dialog box, it will give an opening filter for the current filenames,
  8. 'or the save file types.
  9. Open.Filter = "Text [*.txt*]|*.txt|All Files [*.*]|*.*"
  10. 'it checks if the file is exist or not
  11. Open.CheckFileExists = True
  12. 'sets the openfile dialog name as "OpenFile"
  13. Open.Title = "OpenFile"
  14. Open.ShowDialog(Me)
  16. Try
  17. 'it opens the selected file by the user
  18. Open.OpenFile()
  19. 'opens the existing file
  20. myStreamReader = System.IO.File.OpenText(Open.FileName)
  21. 'it reads the streams from current position to the end of position and display the result to RichTextBox as Text
  22. RichTextBox1.Text = myStreamReader.ReadToEnd()
  23. Catch ex As Exception
  24. 'it will catch if any errors occurs
  25. MsgBox(ex.Message, MsgBoxStyle.Information)
  26. End Try

The code above uses a StreamReader, The StreamReader is an object available to System. IO to read streams of text. The stream is basically the sequence of bytes passing through the communication path. Then two main streams are: the input stream that is used for reading data from file (read operation) and the output stream is used for writing into a file (write operation).

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.