Visual Basic Reading/Writing Text Files

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:
  1. Imports 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.
  1. Class Form1
  2.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  3.         Dim fo As New OpenFileDialog
  4.         fo.RestoreDirectory = True
  5.         fo.Filter = "txt files (*.txt)|*.txt"
  6.         fo.FilterIndex = 1
  7.         fo.ShowDialog()
  8.         If (fo.FileName = Nothing) Then
  9.             MsgBox("Error: No path selected")
  10.         Else
  11.             TextBox2.Text = fo.FileName
  12.             Using sr As New streamreader(fo.FileName)
  13.                 TextBox1.Text = sr.readtoend()
  14.             End Using
  15.         End If
  16.     End Sub
  17. End Class
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:
  1.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  2.         If (TextBox2.Text = Nothing) Then
  3.             MsgBox("Please read a file first!")
  4.         Else
  5.             Dim res = MsgBox("Do you want to save to the same file?", MsgBoxStyle.YesNo, "Overwrite Read File?")
  6.             If (res = MsgBoxResult.Yes) Then
  7.                 If (My.Computer.FileSystem.FileExists(TextBox2.Text)) Then
  8.                     Using sw As New StreamWriter(TextBox2.Text)
  9.                         sw.Write(TextBox1.Text)
  10.                     End Using
  11.                 End If
  12.             Else
  13.                 Dim fs As New SaveFileDialog
  14.                 fs.RestoreDirectory = True
  15.                 fs.Filter = "txt files (*.txt)|*.txt"
  16.                 fs.FilterIndex = 1
  17.                 fs.ShowDialog()
  18.                 If (fs.FileName = Nothing) Then
  19.                     MsgBox("Error: No path selected")
  20.                 Else
  21.                     Using sw As New StreamWriter(fs.FileName)
  22.                         sw.Write(TextBox1.Text)
  23.                     End Using
  24.                 End If
  25.             End If
  26.         End If
  27.     End Sub
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!
  1. Imports System.IO
  2. Class Form1
  3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  4.         Dim fo As New OpenFileDialog
  5.         fo.RestoreDirectory = True
  6.         fo.Filter = "txt files (*.txt)|*.txt"
  7.         fo.FilterIndex = 1
  8.         fo.ShowDialog()
  9.         If (fo.FileName = Nothing) Then
  10.             MsgBox("Error: No path selected")
  11.         Else
  12.             TextBox2.Text = fo.FileName
  13.             Using sr As New streamreader(fo.FileName)
  14.                 TextBox1.Text = sr.readtoend()
  15.             End Using
  16.         End If
  17.     End Sub
  18.  
  19.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  20.         If (TextBox2.Text = Nothing) Then
  21.             Dim fs As New SaveFileDialog
  22.             fs.RestoreDirectory = True
  23.             fs.Filter = "txt files (*.txt)|*.txt"
  24.             fs.FilterIndex = 1
  25.             fs.ShowDialog()
  26.             If (fs.FileName = Nothing) Then
  27.                 MsgBox("Error: No path selected")
  28.             Else
  29.                 Using sw As New StreamWriter(fs.FileName)
  30.                     sw.Write(TextBox1.Text)
  31.                 End Using
  32.             End If
  33.         Else
  34.             Dim res = MsgBox("Do you want to save to the same file?", MsgBoxStyle.YesNo, "Overwrite Read File?")
  35.             If (res = MsgBoxResult.Yes) Then
  36.                 If (My.Computer.FileSystem.FileExists(TextBox2.Text)) Then
  37.                     Using sw As New StreamWriter(TextBox2.Text)
  38.                         sw.Write(TextBox1.Text)
  39.                     End Using
  40.                 End If
  41.             Else
  42.                 Dim fs As New SaveFileDialog
  43.                 fs.RestoreDirectory = True
  44.                 fs.Filter = "txt files (*.txt)|*.txt"
  45.                 fs.FilterIndex = 1
  46.                 fs.ShowDialog()
  47.                 If (fs.FileName = Nothing) Then
  48.                     MsgBox("Error: No path selected")
  49.                 Else
  50.                     Using sw As New StreamWriter(fs.FileName)
  51.                         sw.Write(TextBox1.Text)
  52.                     End Using
  53.                 End If
  54.             End If
  55.         End If
  56.     End Sub
  57. End Class

Add new comment