StreamWriter Appending not Overwriting

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
 
T

Teemu Keiski

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)
 
M

mosscliffe

Thank You - tracking down the exact Syntax, can be difficult at times.
I am always grateful to the people, who help in this newsgroup.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top