Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I get the following error - Cannot access a closed file - within the following code at the line highlighted red.

 

'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.

  • 5 years later...
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...