SaveFileDialog in Visual Basic .NET

Last Updated: March 19, 2020

SaveFileDialog is another important dialog box which can be used to save file

This is the code used to save the text of the TextBox to TXT file

Public Class Form1

    Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
        With SaveFileDialog1
            .DefaultExt = "txt"
            .OverwritePrompt = True
            .Filter = "Text Dcoment (*.txt)|*.*"

        End With

        If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then

            Dim sw As New System.IO.StreamWriter(SaveFileDialog1.FileName)

        End If
    End Sub
End Class

Code explanation

  • Line 5 : Default extension is txt. If you do not specify the file extension when you save the file, default one will be taken
  • Line 6 : If the file is already there it will ask for overwrite dialog as shown in below image


  • Line 7 : Using filter for text files
  • Line 11: Check for Windows.Forms.DialogResult.OK to execute the code
  • Line 13: Open the StreamWriter and pass the file name parameter come with SaveFileDialog1.FileName
  • Line 14: Write the Text to Stream