5

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mac
  • 63
  • 1
  • 2
  • 5

1 Answers1

9

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.

Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79