Read Contents of a Text File using BinaryReader in VB.NET

Today in VB.NET, I'm going to teach you how to read and view contents of a text file using Binary Reader. I made this tutorial before but it is created using StreamReader. See here: How to Read and Write Text Files in VB.NET Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. 2. Next, add an OpenFileDialog named OpenFileDialog1 for browsing text files, two textboxes named TextBox1 for displaying the filename of the text file and TextBox2 for displaying the content of that text file. Insert also two buttons named Button1 for browsing a text file, and Button2 for displaying its content. You must design your interface like this: design 3. Import the System.IO namespace to access the FileStream and BinaryReader class.
  1. Imports System.IO
4. For browsing text files, put this code in your Button1.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Dim OpenFileDialog1 As New OpenFileDialog
  3.  
  4. OpenFileDialog1.Filter = "Text Files (*)|*.txt"
  5. If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  6. TextBox1.Text = OpenFileDialog1.FileName
  7. End If
The code above filters the browsing of file into text files only, meaning the .txt extension file. After choosing the desired text file, then it will display the path into TextBox1. 5. For reading the contents of the text file, put this code in your Button2.
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2.  
  3. ' initialize filestream for accessing text files and BinaryReader for viewing its content
  4. Dim fs As System.IO.FileStream
  5. Dim br As System.IO.BinaryReader
  6. 'initialize buffered array and lenth of the file
  7. Dim buffer(100) As Char
  8. Dim mylength As Long
  9. 'access the text file in your textbox1 using FileStream
  10. fs = New System.IO.FileStream(TextBox1.Text, IO.FileMode.OpenOrCreate)
  11. 'read the content of your textfile in the path inputted
  12. br = New System.IO.BinaryReader(fs)
  13. ' make length equal to the text file
  14. mylength = fs.Length
  15. If mylength > 100 Then
  16. mylength = 100
  17. End If
  18. 'read the contents of the text file
  19. br.Read(buffer, 0, mylength)
  20. 'display the output in textbox2
  21. TextBox2.Text = buffer
  22. 'close the file
  23. br.Close()
  24. fs.Close()
  25. End Sub
Full source code:
  1. Imports System.IO
  2.  
  3. Public Class Form1
  4.  
  5. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6. Dim OpenFileDialog1 As New OpenFileDialog
  7.  
  8. OpenFileDialog1.Filter = "Text Files (*)|*.txt"
  9. If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  10. TextBox1.Text = OpenFileDialog1.FileName
  11. End If
  12. End Sub
  13.  
  14. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  15.  
  16. ' initialize filestream for accessing text files and BinaryReader for viewing its content
  17. Dim fs As System.IO.FileStream
  18. Dim br As System.IO.BinaryReader
  19. 'initialize buffered array and lenth of the file
  20. Dim buffer(100) As Char
  21. Dim mylength As Long
  22. 'access the text file in your textbox1 using FileStream
  23. fs = New System.IO.FileStream(TextBox1.Text, IO.FileMode.OpenOrCreate)
  24. 'read the content of your textfile in the path inputted
  25. br = New System.IO.BinaryReader(fs)
  26. ' make length equal to the text file
  27. mylength = fs.Length
  28. If mylength > 100 Then
  29. mylength = 100
  30. End If
  31. 'read the contents of the text file
  32. br.Read(buffer, 0, mylength)
  33. 'display the output in textbox2
  34. TextBox2.Text = buffer
  35. 'close the file
  36. br.Close()
  37. fs.Close()
  38. End Sub
  39. End Class
Output: output Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment