How to Write Data to a Text File using BinaryWriter in VB.NET
Submitted by donbermoy on Wednesday, June 18, 2014 - 07:40.
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:
3. For Button1 as browsing and choosing a text file button, put this code below.
4. For Button2 as writing data to a text file, put this code below.
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:
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
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- 'initialize an openfiledialog for browsing text files
- Dim OpenFileDialog1 As New OpenFileDialog
- 'filter text files only
- OpenFileDialog1.Filter = "Text Files (*)|*.txt"
- 'after choosing a desired text files, it will display its path on textbox1
- If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
- TextBox1.Text = OpenFileDialog1.FileName
- End If
- Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
- 'initialize filestream and binarywriter
- Dim fs As System.IO.FileStream
- Dim bw As System.IO.BinaryWriter
- 'finding and accessing the filepath displayed in textbox1
- fs = New System.IO.FileStream(TextBox1.Text, IO.FileMode.OpenOrCreate)
- 'the binarywriter holds the filestream after accessing the path
- bw = New System.IO.BinaryWriter(fs)
- ' the binarywriter seeks the origin of the text file
- bw.Seek(0, System.IO.SeekOrigin.Begin)
- ' the binary writer writes the inputted string in textbox2
- bw.Write(TextBox2.Text)
- ' prompts the user that the writing of data has been done
- MessageBox.Show("Done writing data to the text file.")
- 'close the filestream and binarywriter
- End Sub
Add new comment
- 245 views