Solved

Possible to reopen a closed filestream?

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Opening and Closing Connections and Data Adapters 10 49
VB.Net How to Exit Sub - Exit Form??? 5 57
VS.net 2010 11 32
get row value in vb.net 4 14
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

808 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