How to rewrite text in VB.NET

Once I learned how to add a text file using the following code, but how can I overwrite a file every time I press the button alone (no one taught me)?

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim ALPHAVAL As String = "C:\ALPHAVAL.txt"

    If System.IO.File.Exists(ALPHAVAL) = True Then
        Dim objWriter As New System.IO.StreamWriter(ALPHAVAL, True)
        objWriter.WriteLine(TextBox1.Text)
        objWriter.Close()
    End If

End of class

+3
source share
1 answer

The signature of the StreamWriter constructor is as follows:

public StreamWriter(string path,bool append  )

So change your code:

System.IO.StreamWriter(ALPHAVAL, True)

to:

 System.IO.StreamWriter(ALPHAVAL, False)

This tells StreamWriter to overwrite the file.

+8
source

Source: https://habr.com/ru/post/1792809/


All Articles