vb.netwinformssavesavefiledialogwritealltext

How to create a text file and append text to the existing file?


I'm trying to save my textbox1 content into a text file to my computer.
What I want to do is to create a save directory and when it's set up, I will save the content of textbox1 to that txt file.
Not just once, I want to append the content of the TextBox to the same file after.

Button2: Trying to browse and create a txt file.
Button4 (1st click): This button will save the content of textbox1 to the txt file created.
Button4 (2nd click)": this will add the current content of textbox1 to the same txt file.

But I want to be able to change the directory whenever I want.
I also want to choose path outside the code or outside the textbox.
Meaning, I want a button that will let me choose a folder where I want to create a text file.
The 2nd button will let me save the textbox1 content to the created text file.

Here's some of my code but I don't know if I'm doing it correctly because it's now doing what I want. Please help.

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim isave As New SaveFileDialog
        isave.Filter = "txt files (*.txt) |*.txt"
        isave.FilterIndex = 2
        isave.RestoreDirectory = False

        If isave.ShowDialog() = DialogResult.OK Then
            IO.File.WriteAllText(isave.FileName, TextBox1.Text)
        End If
    End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim theText As String
        theText = TextBox1.Text
        IO.File.AppendAllText("isave", Environment.NewLine & theText)
End Sub

Solution

  • A possible way to proceed:

    I've renamed the Buttons to SaveFile and UpdateFile and the TextBox to TextContent: it's always better to assign meaningful names to your Controls

    The SaveFileDialog object must be declared with an Using statement, since you need to dispose of it when it's closed (a window shown with ShowDialog() cannot dispose of itself. Declaring a disposable object with an Using statement, ensures that the object is disposed even if an exception is raised in the meanwhile; most of the times)


    Imports System.IO
    
    Private lastSaveFileName As String = String.Empty
    
    Private Sub SaveFile_Click(sender As Object, e As EventArgs) Handles SaveFile.Click
        lastSaveFileName = GetSaveFileName(lastSaveFileName)
        If Not String.IsNullOrEmpty(lastSaveFileName) Then
            File.WriteAllText(lastSaveFileName, TextContent.Text)
        End If
    End Sub
    
    Private Sub UpdateFile_Click(sender As Object, e As EventArgs) Handles UpdateFile.Click
        If Not String.IsNullOrEmpty(lastSaveFileName) AndAlso File.Exists(lastSaveFileName) Then
            File.AppendAllText(lastSaveFileName, Environment.NewLine & TextContent.Text)
        Else
            MessageBox.Show(
                Me, "The file has not been created yet [other instructions]",
                "Save file missing", MessageBoxButtons.OK, MessageBoxIcon.Information
            )
        End If
    End Sub
    
    Private Function GetSaveFileName(suggestedName As String) As String
        Using sfd As New SaveFileDialog()
            sfd.Filter = "Text Files (*.txt) |*.txt"
            sfd.FileName = suggestedName
            sfd.OverwritePrompt = True
            If sfd.ShowDialog() = DialogResult.OK Then
                Return sfd.FileName
            End If
            Return String.Empty
        End Using
    End Function