Visual Basic Reading/Writing Text Files
Submitted by Yorkiebar on Saturday, September 7, 2013 - 09:31.
Introduction:
As you can see from the title, this is a tutorial on simply reading and writing Text Files. Let's begin.
Steps of Creation:
Step 1:
This will require one Import which is to enable us to read and write files. The Import is System.IO:
Step 2:
The design this program is going to need is to have one textbox to contain the file contents, another textbox to store the current file path, one button to read a file and another to save the file. The naming of these are:
Textbox1 = Textbox containing file contents
Textbox2 = File Path
Button1 = Read
Button2 = Write
I would also recommend setting ReadOnly to True on textbox2 so the user can not edit the file path.
Step 3:
Now, on to the code!
First let's create the read code, double click on the read button to get our click event generated.
As you can see we create a new variable called "fo" which is set to a New OpenFileDialog. Then we set some properties of this dialog. The RestoreDirectory means whenever it opens the starting folder will be the last visited directory from any Windows Explorer/Browser you have used. We then set the file types which can be opened, I only used .txt but you can also set things like .csv, after that we set the default file type to .txt (my only file type). We then display the dialog box to the user.
After the user selects a file to read we then check whether the file is set to nothing (If they closed the box) and display an error message if there is no file selected. If there is a file selected we set the path to textbox2 and use a StreamReader with the path set to our selected file and read the file to our textbox1.
Step 4:
That's it for the reading part, now for the writing part:
This is basically the same code, but the opposite - obviously. First we check to see if the path is set in textbox2 from our read script. If there is no path set it means there is no file open so we display a New SaveFileDialog for them to select a new file to save their text as. If there is a file open we give them the option to either overwrite the read file, or, create a new file. Whichever one they choose we create a StreamWriter with the path address of the selected path (Textbox2 or SaveFileDialog.FileName) and write textbox1 text to it.
Project Complete!
That's it! Below is the source code to the whole project along with a download in the code section, Thank you!
- Imports System.IO
- Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim fo As New OpenFileDialog
- fo.RestoreDirectory = True
- fo.Filter = "txt files (*.txt)|*.txt"
- fo.FilterIndex = 1
- fo.ShowDialog()
- If (fo.FileName = Nothing) Then
- MsgBox("Error: No path selected")
- Else
- TextBox2.Text = fo.FileName
- Using sr As New streamreader(fo.FileName)
- TextBox1.Text = sr.readtoend()
- End Using
- End If
- End Sub
- End Class
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- If (TextBox2.Text = Nothing) Then
- MsgBox("Please read a file first!")
- Else
- Dim res = MsgBox("Do you want to save to the same file?", MsgBoxStyle.YesNo, "Overwrite Read File?")
- If (res = MsgBoxResult.Yes) Then
- If (My.Computer.FileSystem.FileExists(TextBox2.Text)) Then
- Using sw As New StreamWriter(TextBox2.Text)
- sw.Write(TextBox1.Text)
- End Using
- End If
- Else
- Dim fs As New SaveFileDialog
- fs.RestoreDirectory = True
- fs.Filter = "txt files (*.txt)|*.txt"
- fs.FilterIndex = 1
- fs.ShowDialog()
- If (fs.FileName = Nothing) Then
- MsgBox("Error: No path selected")
- Else
- Using sw As New StreamWriter(fs.FileName)
- sw.Write(TextBox1.Text)
- End Using
- End If
- End If
- End If
- End Sub
- Imports System.IO
- Class Form1
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim fo As New OpenFileDialog
- fo.RestoreDirectory = True
- fo.Filter = "txt files (*.txt)|*.txt"
- fo.FilterIndex = 1
- fo.ShowDialog()
- If (fo.FileName = Nothing) Then
- MsgBox("Error: No path selected")
- Else
- TextBox2.Text = fo.FileName
- Using sr As New streamreader(fo.FileName)
- TextBox1.Text = sr.readtoend()
- End Using
- End If
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- If (TextBox2.Text = Nothing) Then
- Dim fs As New SaveFileDialog
- fs.RestoreDirectory = True
- fs.Filter = "txt files (*.txt)|*.txt"
- fs.FilterIndex = 1
- fs.ShowDialog()
- If (fs.FileName = Nothing) Then
- MsgBox("Error: No path selected")
- Else
- Using sw As New StreamWriter(fs.FileName)
- sw.Write(TextBox1.Text)
- End Using
- End If
- Else
- Dim res = MsgBox("Do you want to save to the same file?", MsgBoxStyle.YesNo, "Overwrite Read File?")
- If (res = MsgBoxResult.Yes) Then
- If (My.Computer.FileSystem.FileExists(TextBox2.Text)) Then
- Using sw As New StreamWriter(TextBox2.Text)
- sw.Write(TextBox1.Text)
- End Using
- End If
- Else
- Dim fs As New SaveFileDialog
- fs.RestoreDirectory = True
- fs.Filter = "txt files (*.txt)|*.txt"
- fs.FilterIndex = 1
- fs.ShowDialog()
- If (fs.FileName = Nothing) Then
- MsgBox("Error: No path selected")
- Else
- Using sw As New StreamWriter(fs.FileName)
- sw.Write(TextBox1.Text)
- End Using
- End If
- End If
- End If
- End Sub
- End Class
Add new comment
- 361 views