469,625 Members | 1,801 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,625 developers. It's quick & easy.

Saving StreamWriter buffer before flushing in vb.net code

Hello, I have a VB.NET code that creates a StreamWriter file first and
then it does a series of procedures that write lines for this
StreamWriter. When all the procedures are done, the StreamWriter is
flushed and closed and all the lines get written to the text.

However, if for some reason one of the procedures fails. The
StreamWriter doesn't write anything at all and the file ends up empty.
Is there a way to save (or actually write) the lines stored in the
buffer when each procedure is taken care of? The following is the
pseudocode I'm using.

' Setting up the log and write date and time of process startup
Dim sw As New StreamWriter("c:\mylog.txt", True)

sw.WriteLine("The Process starts here.")
sw.writeline("start of procedure 1")
procedure1
sw.writeline("end of procedure 1")

<<<< - need to save StreamWriter buffer (all the sw.writelines
created since last save) here!

sw.writeline("start of procedure 2")
procedure2
sw.writeline("end of procedure 2")

<<<< - need to save StreamWriter buffer (all the sw.writelines
created since last save) here!

sw.writeline("start of procedure 3")
procedure3
sw.writeline("end of procedure 3")

<<<< - need to save StreamWriter buffer (all the sw.writelines
created since last save) here!
' flush the stream writer
sw.Flush()
' close the stream writer
sw.Close()
sw = Nothing
As you can see, if nothing bad happens, the streamwriter works fine.
However, if something in any of the procedures fails, the streamwriter
file ends up empty.

As always, any help would be tremendoulsy appreciated it!

Aug 28 '06 #1
1 3785
Hello Max,

Are you a parrot? Do you just copy and paste other people's code without
understanding it? RTFM.

-Boo
Hello, I have a VB.NET code that creates a StreamWriter file first
and then it does a series of procedures that write lines for this
StreamWriter. When all the procedures are done, the StreamWriter is
flushed and closed and all the lines get written to the text.

However, if for some reason one of the procedures fails. The
StreamWriter doesn't write anything at all and the file ends up empty.
Is there a way to save (or actually write) the lines stored in the
buffer when each procedure is taken care of? The following is the
pseudocode I'm using.

' Setting up the log and write date and time of process startup Dim sw
As New StreamWriter("c:\mylog.txt", True)

sw.WriteLine("The Process starts here.")

sw.writeline("start of procedure 1")
procedure1
sw.writeline("end of procedure 1")
<<<< - need to save StreamWriter buffer (all the sw.writelines
created since last save) here!

sw.writeline("start of procedure 2")
procedure2
sw.writeline("end of procedure 2")
<<<< - need to save StreamWriter buffer (all the sw.writelines
created since last save) here!

sw.writeline("start of procedure 3")
procedure3
sw.writeline("end of procedure 3")
<<<< - need to save StreamWriter buffer (all the sw.writelines
created since last save) here!

' flush the stream writer
sw.Flush()
' close the stream writer
sw.Close()
sw = Nothing
As you can see, if nothing bad happens, the streamwriter works fine.
However, if something in any of the procedures fails, the
streamwriter file ends up empty.

As always, any help would be tremendoulsy appreciated it!

Aug 28 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by José Joye | last post: by
13 posts views Thread by Rob Corwin | last post: by
2 posts views Thread by Kevien Lee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.