On Fri, 16 May 2008 13:08:07 -0700, <Ad*********@gmail.comwrote:
Not sure which part is cryptic. I need to open the file for writing
and keep it open in a loop.
You keep saying "the file", but then you also imply that you may have more
than one file.
I can close and/or dispose the object but I want to keep the file open
for writing instead of making roundtrip opens/closes each time.
If you have more than one file you might open in the loop, then you need
to keep track of all the files you've opened. You've already been
provided suggestions for doing that.
If you only have one file, then I recommend putting the FileStream
variable outside the loop, initialized to null, and creating the
FileStream instance only if the variable is null. If it's not null, just
use the current value of the variable.
Plus,
i would have to advance the cursor to the end of the file each time I
open it, which could be big.
The size of the file make absolutely no difference with respect to
seeking. You could in fact close and reopen the file each iteration of
the loop, seeking to the end of the file before writing each time (or just
use FileMode.Append instead of OpenOrCreate). The only real performance
overhead would be the act of opening and closing the file; the seeking
isn't a problem at all.
But the fact is, there's not any need to reopen the file each time through
the loop. Whether you have more than one file or not, there is a
perfectly good solution that doesn't involve reopening the file with each
iteration of the loop.
Pete