How to Write Data to a Text File using BinaryWriter in VB.NET

Today in VB.NET, I will teach you how to create a program that will write data to a textfile using BinaryWriter. I already made this kind of tutorial but it has created using StreamWriter. See here: 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 two Buttons named Button1 for browsing and choosing a text file and Button2 for writing data on it. Insert also two textboxes named TextBox1 for displaying the path of the textfile and TextBox2 for inputting a data on it. Add also an OpenFileDialog named OpenFileDialog1 for browsing of text files. You must design your interface like this: design 3. For Button1 as browsing and choosing a text file button, put this code below.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.  
  3. 'initialize an openfiledialog for browsing text files
  4. Dim OpenFileDialog1 As New OpenFileDialog
  5. 'filter text files only
  6. OpenFileDialog1.Filter = "Text Files (*)|*.txt"
  7. 'after choosing a desired text files, it will display its path on textbox1
  8. If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
  9. TextBox1.Text = OpenFileDialog1.FileName
  10. End If
4. For Button2 as writing data to a text file, put this code below.
  1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  2. 'initialize filestream and binarywriter
  3. Dim fs As System.IO.FileStream
  4. Dim bw As System.IO.BinaryWriter
  5.  
  6. 'finding and accessing the filepath displayed in textbox1
  7. fs = New System.IO.FileStream(TextBox1.Text, IO.FileMode.OpenOrCreate)
  8. 'the binarywriter holds the filestream after accessing the path
  9. bw = New System.IO.BinaryWriter(fs)
  10. ' the binarywriter seeks the origin of the text file
  11. bw.Seek(0, System.IO.SeekOrigin.Begin)
  12. ' the binary writer writes the inputted string in textbox2
  13. bw.Write(TextBox2.Text)
  14. ' prompts the user that the writing of data has been done
  15. MessageBox.Show("Done writing data to the text file.")
  16. 'close the filestream and binarywriter
  17. bw.Close()
  18. fs.Close()
  19. End Sub
5. Open notepad. Create an empty text file named data.txt. Choose this file to browse in the program. Press F5 to run the program. Output: outputoutput For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. 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