Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Possible to reopen a closed filestream?

Posted on 2004-08-11
4
Medium Priority
?
2,450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

688 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