Cannot access a closed file.

nibsy

Well-known member
Joined
Aug 7, 2002
Messages
51
Location
Kent, UK
I get the following error - Cannot access a closed file - within the following code at the line highlighted red.

Code:
Processing.
                    For a = 1 To iNumDays Step 1

                        sFile = String.Concat("access.Q", sDate7)
                        sReadFile = String.Concat(sPath, sFile)
                        If a = 1 Then
                            If File.Exists(sReadFile) Then
                                sContents = New ArrayList()
                                Open file.
                                fsInput = New FileStream(sReadFile, FileMode.Open, FileAccess.Read)
                                srInput = New StreamReader(fsInput)

                                Read File.
                                sFileLine = srInput.ReadLine()
                                While Not IsNothing(sFileLine)
                                    Append to array.
                                    sContents.Add(sFileLine)
                                    Read File.
                                    sFileLine = srInput.ReadLine()
                                End While

                                srInput.Close()
                                fsInput.Close()

                                Write contents of read file to output file.
                                sWriteFile = String.Concat(sPath, sName)
                                fsOutput = New FileStream(sWriteFile, FileMode.Create, FileAccess.Write)
                                srOutput = New StreamWriter(fsOutput)

                                For i = 0 To sContents.Count - 1
                                    Write File.
                                    srOutput.WriteLine(sContents(i))
                                Next

                                srOutput.Close()
                                fsOutput.Close()

                                Derive the following day.
                                If iNumDays > 1 Then
                                    dNewDate = DateAdd(DateInterval.Day, 1, dDate)
                                    sDate8 = Format(dNewDate, "yyyyMMdd")
                                    sDate7 = CStr(sDate8 - 19000000)
                                End If
                            Else
                                MessageBox.Show(String.Concat("Could not open file.  Make sure file ", sFile, " exists."), "Error Message")
                                Exit loop!
                                a = iNumDays
                            End If
                        Else
                            Clear down array (for re-use).
                            sContents.Clear()
                            If File.Exists(sReadFile) Then

                                Open file.
                                fsInput = New FileStream(sReadFile, FileMode.Open, FileAccess.Read)
                                srInput = New StreamReader(fsInput)

                                Read File.
                                sFileLine = srInput.ReadLine()
                                While Not IsNothing(sFileLine)
                                    Append to array.
                                    sContents.Add(sFileLine)
                                    Read Line.
                                    sFileLine = srInput.ReadLine()
                                End While

                                fsInput.Close()
                                srInput.Close()

                                Append contents of read file to output file.
                                fsOutput = New FileStream(sWriteFile, FileMode.Append, FileAccess.Write)
                                srOutput = New StreamWriter(fsOutput)

                                For i = 0 To sContents.Count - 1
                                    Write File.
                                    srOutput.WriteLine(sContents(i))
                                Next

                                fsOutput.Close()
                                [color=red]srOutput.Close()[/color]
                            Else
                                MessageBox.Show(String.Concat("Could not open file.  Make sure file ", sFile, " exists."), "Error Message")
                            End If
                        End If
                    Next a

Can anybody tell me why and give me a possible remedy?
Many Thanks.
 
I fixed my problem by suppressing the Finalize on the base stream after I opened the file

Here is the Line I added after opening the file
GC.SuppressFinalize(myLogStream.BaseStream);

Thsn at the class distructor I could Flush and Close the file
 
Back
Top