Solved

Possible to reopen a closed filestream?

Posted on 2004-08-11
4
2,216 Views
Last Modified: 2008-01-09
Is it possible to reopen filestream object after it has been closed? What I am trying to do is combine to static text files together and write a few static lines in between. I am able to combine the two files in one new file but when I attempt to add a textwriter stream in between to write out my dynamic lines I get the error:

"The process cannot access the file "path to file" because it is being used by another process."

If I try to close the filestream before using the textwriter I am access the new files filestream to complete the combination of the two files receiving the error:

"Cannot access a closed file. "

Here is my existing code, hacked apart as it may be, for a better understanding of what I am trying to do:

=====================================================================
        Dim fn1 As String = "c:\temp\ini_files\ini_part_1.txt"
        Dim fn2 As String = "c:\temp\ini_files\ini_part_2.txt"
        Dim fn3 As String = "c:\temp\ini_files\ini_part_3.txt"

        Dim f1 As New FileStream(fn1, FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim f2 As New FileStream(fn2, FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim f3 As New FileStream(fn3, FileMode.Create, FileAccess.Write, FileShare.Read)

        ' Create a reader that can read bytes from the FileStream.
        Dim sr1 As New BinaryReader(f1)
        ' While not at the end of the file, read lines from the file.
        While sr1.PeekChar() > -1
            Dim input As Byte = sr1.ReadByte()
            f3.WriteByte(CType(input, Byte))
        End While
        sr1.Close()
        f3.Close()

       'Call function to write out dynamic content
        MyTools.WriteDynamicInfo(Session("strField1"), Session("strField2"))

        ' Create a reader that can read bytes from the FileStream.
        Dim sr2 As New BinaryReader(f2)
        ' While not at the end of the file, read lines from the file.
        While sr2.PeekChar() > -1
            Dim input As Byte = sr2.ReadByte()
            f3.WriteByte(CType(input, Byte))
        End While
        sr2.Close()

        f1.Close()
        f2.Close()
        f3.Close()
=====================================================================

Any suggestions on a more efficient approach to this procedure?

Thanks
0
Comment
Question by:GCaron
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
DotNetLover_Baan earned 500 total points
ID: 11778151
Hi there...
open f3 on Append mode..
Dim f3 As New FileStream(fn3, FileMode.Create, FileAccess.Append, FileShare.Read)

First write the first file into it.
Then append your own lines..
Then Apeend the second file into it.

-Baan
0
 
LVL 19

Expert Comment

by:drichards
ID: 11778316
You can change the share access to "ReadWrite" instead of "Read", or you can pass the open writer to the other routine where you are writing the middle lines.
0
 
LVL 19

Expert Comment

by:drichards
ID: 11778328
Also be sure to do second open in "Append" mode or you wipe out previous content.
0
 

Author Comment

by:GCaron
ID: 11787414
In the end I ended up writing out my dynamic lines to a third text file then used he FileStream to read in the three text files in order to create a new text file.

Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now