Opening files for output

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I typically open a file for writing like this:

Public stLog As Stream
Public swLog As StreamWriter

........

stLog = File.Open("c:\temp\Errors.log", FileMode.Append)
swLog = New StreamWriter(stLog)

But, what if I didn't know until runtime how many output files I needed to
open?
For example, what if there was a listbox with a list of filenames that need
to be opened for output. How would I create and set the variables to
initialized stream and streamwriter classes?

Thanks for any help!
Mark
 
Mark,

You should try not to set this public
Public stLog As Stream
Public swLog As StreamWriter
stLog = File.Open("c:\temp\Errors.log", FileMode.Append)
swLog = New StreamWriter(stLog)

Better is to open and close it in the method where you have your actions
(2003 code) roughly typed.

\\\
Private sub MyMethod(byval towrite as string)
Dim stLog As FileStream = New FileStream(("c:\temp\Errors.log",
FileMode.Append)
Dim swLog As New StreamWriter(stLog)
swLog.Write(towrite)
swLog.Close
stLog.Close
End sub
///

Every time this goes out of scope, everything that is in it created, will be
deleted. Don't worry about the time this takes, it is about nothing, however
secures a good memory management and fixes your problem.

I hope this helps?

Cor
 
This is great! Thanks.

Mark

Cor Ligthert said:
Mark,

You should try not to set this public

Better is to open and close it in the method where you have your actions
(2003 code) roughly typed.

\\\
Private sub MyMethod(byval towrite as string)
Dim stLog As FileStream = New FileStream(("c:\temp\Errors.log",
FileMode.Append)
Dim swLog As New StreamWriter(stLog)
swLog.Write(towrite)
swLog.Close
stLog.Close
End sub
///

Every time this goes out of scope, everything that is in it created, will be
deleted. Don't worry about the time this takes, it is about nothing, however
secures a good memory management and fixes your problem.

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top