Solved

Possible to reopen a closed filestream?

Posted on 2004-08-11
4
2,239 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

16 Experts available now in Live!

Get 1:1 Help Now