GCaron
asked on
Possible to reopen a closed filestream?
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_par t_1.txt"
Dim fn2 As String = "c:\temp\ini_files\ini_par t_2.txt"
Dim fn3 As String = "c:\temp\ini_files\ini_par t_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(S ession("st rField1"), 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
"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_par
Dim fn2 As String = "c:\temp\ini_files\ini_par
Dim fn3 As String = "c:\temp\ini_files\ini_par
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(S
' 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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
Also be sure to do second open in "Append" mode or you wipe out previous content.
ASKER
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
Thanks