Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Possible to reopen a closed filestream?

Posted on 2004-08-11
4
Medium Priority
?
2,501 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 1500 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month11 days, 23 hours left to enroll

916 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