StreamWriter Appending not Overwriting

  • Thread starter Thread starter mosscliffe
  • Start date Start date
M

mosscliffe

I have a simple form with a multiline Textbox.

I load the textbox with the contents of a text file.

I make changes to the textbox, then I attempt to OVERWRITE the original
file.

I have tried opening with FilMode.Create, FileMode.Truncate but they
bothh APPEND to the file.

What is the correct syntax for Overwriting - Thanks for any help

My Code is as follows:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim strFname As String = DropDownList1.SelectedItem.Text
lblDebug.Text += "LoadButton: " &
DropDownList1.SelectedItem.Text & "<BR />"
Dim fw As New System.IO.StreamReader(Server.MapPath("MyData/" &
strFname), True)
TextBox1.Text = fw.ReadToEnd()
fw.Close()
fw.Dispose()

End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim strFname As String = DropDownList1.SelectedItem.Text
lblDebug.Text += "UpdateButton: " &
DropDownList1.SelectedItem.Text & "<BR />"
Dim fw As New System.IO.StreamWriter(Server.MapPath("MyData/" &
strFname), System.IO.FileMode.Create)
fw.Write(TextBox1.Text)
fw.Close()
fw.Dispose()

End Sub
 
Create StreamWriter instance with "False" for the second ctor argument. It
specifies if data is appended (True means Append, False means overwrite or
create a new file)

Dim fw As New System.IO.StreamWriter(Server.MapPath("MyData/" & strFname),
False)
 
Thank You - tracking down the exact Syntax, can be difficult at times.
I am always grateful to the people, who help in this newsgroup.
 
Back
Top