×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

StreamWriter overwrites instead of appending

StreamWriter overwrites instead of appending

StreamWriter overwrites instead of appending

(OP)
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 --> .NET

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

RE: StreamWriter overwrites instead of appending

Just change your constructor, so instead of:

CODE -->

Dim loStreamWriter As New StreamWriter(tcFileOut) 

Use

CODE -->

Dim loStreamWriter As New StreamWriter(tcFileOut, True)  ' False to overwrite 

instead

RE: StreamWriter overwrites instead of appending

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

Regards,

Ilya

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close