Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

StreamWriter overwrites instead of appending 1

Status
Not open for further replies.

IlyaRabyy

Programmer
Nov 9, 2010
571
US
Colleagues,
I am new to StreamWriter class, trying to figure out what am I doing wrong (if anything).
The goal/task is: open the existing file (tcFileIn), read the contents and append iCnt times the contents of that lsBlock text to the existing tcFileOut.
Note: File tcOutFile is not empty, it has some header info in it.
But instead of appending, StreamWriter overwrites that existing text in the tcFileOut (with that iCnt times the lsBlock).
Here's the code:
Code:
Dim loStreamReader As StreamReader = New StreamReader(tcFileIn)
Dim iCnt As Long = 0, lsBlock As String = loStreamReader.ReadToEnd()

' Srteam Reader did the job - StreamReader can go!
loStreamReader.Close()
loStreamReader.Dispose()

Dim loStreamWriter As New StreamWriter(tcFileOut)

For iCnt = 1 To tiChkCnt
   loStreamWriter.Write(lsBlock & vbCrLf)
Next

loStreamWriter.Flush()
loStreamWriter.Close()
loStreamWriter.Dispose()
What am I missing?
OR
Is it "feature, not a bug" of the StreamWriter that it has no "Append" option/setting?

TIA!

Regards,

Ilya
 
Just change your constructor, so instead of:

Code:
[COLOR=blue]Dim loStreamWriter As New StreamWriter(tcFileOut)[/color]

Use

Code:
[blue]Dim loStreamWriter As New StreamWriter(tcFileOut, True)  [COLOR=green]' False to overwrite[/color][/blue]

instead
 
Splendid!
It works!
Thank you, colleague!
(Didn't realize there can be more than one constructor, this old, senile fool me [blush] )

Regards,

Ilya
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top