473,322 Members | 1,307 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,322 software developers and data experts.

Opening files for output

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
Nov 21 '05 #1
2 1201
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
Nov 21 '05 #2
This is great! Thanks.

Mark

"Cor Ligthert" wrote:
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

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jim Willis | last post by:
Hi, I have an application that returns a PDF document to the browser (not as a download). The document is returned in a new window. This works fine in most installations, but I have one...
2
by: BB | last post by:
Im trying to write a piece of code to open all files in the current directory, read their contents and write it out to another file. However after the program runs the output files are not there....
1
by: Alfons | last post by:
Hello, I have build a program that can do file transferring between a Windows XP computer and a DOS computer via a serial port. The Windows program I have build in C++ with Visual Studio 6.0....
0
by: Jonathan | last post by:
Hi everyone, I'm having an issue with my Excel streaming. I run my procedure and if I "Save As" when the dialog opens and then open the file I generate, it's perfect. Also, if I choose "Open" when...
1
by: sunil | last post by:
Hi all, I have a web application in which I have to open a "chm" file that exists on the local file system. I am able to open other files such as PDF, DOC files by writing the Response's output...
1
by: jtertin | last post by:
I am currently using the following code to make sure a file is writable (i.e. is not in use) by using the CanWrite method of the FileStream object. Using the code below, the TextWriter is still...
5
by: muwie | last post by:
Hello, I was browsing to see if I could find something similair to my problem. But I couldn't find anything.. I have this script that counts every word in a file. And then also says how many...
34
by: Alexnb | last post by:
Gerhard Häring wrote: No, it didn't work, but it gave me some interesting feedback when I ran it in the shell. Heres what it told me: Traceback (most recent call last): File...
1
navanova
by: navanova | last post by:
Greetings, I have a problem of opening ms word and excel files on my computer. The files are there for a long time. I use to open and modify them. Suddenly, when i try to open the word files, a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.