nibsy Posted November 7, 2002 Posted November 7, 2002 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. Quote
nibsy Posted November 7, 2002 Author Posted November 7, 2002 Fixed by moving the highlighted line above fsOutput.Close(). :confused: Quote
*Gurus* divil Posted November 7, 2002 *Gurus* Posted November 7, 2002 Closing the FileStream closes the StreamWriter implicitly. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
wassif Posted November 12, 2007 Posted November 12, 2007 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.