Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I overwrite text in VB.NET

I once got taught how to append a text file ussing the following code, but how do I overwrite the file every time I press button one (nobody taught me that)?

    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 class

like image 742
Mac Avatar asked Feb 17 '11 14:02

Mac


1 Answers

Signature of StreamWriter Constructor is this:

public StreamWriter(string path,bool append  )

So Change your code :

System.IO.StreamWriter(ALPHAVAL, True)

to :

 System.IO.StreamWriter(ALPHAVAL, False)

That tells StreamWriter to Overwrite the file.

like image 193
Shekhar_Pro Avatar answered Nov 02 '22 18:11

Shekhar_Pro