43. Saving Text Files

In the previous tutorial, we covered reading the text inside of a text file. In this tutorial we cover how to save a string into a text file using the StreamWriter.

Code

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If System.IO.File.Exists(TextBox1.Text) = True Then
            MessageBox.Show("That file does not exist!")
        Else
            Dim objwriter As New System.IO.StreamWriter(TextBox1.Text)
            objwriter.Write(TextBox2.Text)
            objwriter.Close()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Process.Start(TextBox1.Text)
    End Sub

End Class