473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 3970
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: José Joye | last post by:
I have 2 threads using the same StreamWriter (obtained from FileInfo.AppendText). This first thread will always use the WriteLine() method and the second thread will always use the Flush() method....
13
by: Rob Corwin | last post by:
Hi, a c# app of mine that parses 30,000 xml files writes large amounts of data to file (> 2GB) using a streamwriter object (sw). everything works fine except that the memory used by the app grows...
4
by: Dan | last post by:
In the following example, is it necessary to close the FileStream object as well as the StreamWriter object? FileStream fs = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write,...
10
by: Oscar Thornell | last post by:
Hi, I generate and temporary saves a text file to disk. Later I upload this file to Microsoft MapPoint (not so important). The file needs to be in UTF-8 encoding and I explicitly use the...
4
by: David Buchan | last post by:
Hello, I wonder if anyone could help me. I'm using vb.NET and I'd like to read a binary file, byte by byte, and then write to another file (making a duplicate, identical file). I'd then...
4
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
2
by: Kevien Lee | last post by:
Hi , I had a strang problam ,when i use StreamWriter to append to a file,i found that if i don't close the StreamReader it couldn't write the data into file,the code as folllow that: class...
5
by: arnuld | last post by:
this is from mentioned section. i did not understand some things here: it means "flushing the buffer" and "writing to output device" are SAME thing, these are just 2 different names for the...
1
by: MSwanston | last post by:
Hi I need some help with saving retreiving data from the cache, and how best to structure my code. FYI am working in VS2005/under .NET2 Framework. Ok, we have a series of reports that get run via...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.