Saving StreamWriter buffer before flushing in vb.net code

  • Thread starter Thread starter Max Powers
  • Start date Start date
M

Max Powers

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!
 
Hello Max,

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

-Boo
 
Back
Top