Cannot Access a Closed File

nathani

New member
Joined
Aug 20, 2009
Messages
4
Programming Experience
1-3
I can't understand why I'm getting this error. I'm trying to parse a tab seperated file. After 10 lines being read it throws the above error which is an ObjectDisposedException.

VB.NET:
   Public Function readNextLine(ByRef buffer As Single()) As Boolean
      Dim i As Integer
      Dim tempString As String()

      If Not (fileNfo.Exists) Then
         MsgBox("File does not exist, it has been moved or deleted")
         readNextLine = False
      ElseIf (fStream Is Nothing) Then
         MsgBox("There was a problem reading the file, please try again")
         readNextLine = False
      ElseIf (fStreamReader Is Nothing) Then
         MsgBox("There was a problem reading the file, please try again")
         readNextLine = False
      Else
         tempString = Split(fStreamReader.ReadLine(), cDelim) 'Error thrown here

         ReDim buffer(tempString.Length - 2)
         For i = 0 To tempString.Length - 2
            If (IsNumeric(tempString(i))) Then
               buffer(i) = CSng(tempString(i))
            Else
               buffer(i) = 0
            End If
         Next
         readNextLine = True
      End If
   End Function

The above function is called through the following while loop:

VB.NET:
   Private Sub file_to_dataset(ByVal fileInfo As System.IO.FileInfo)
      Dim singleRead As Single() = {0}

      If Not (fileInfo.Exists) Then Exit Sub 'File does not exist

      If (fileParser Is Nothing) Then
         fileParser = New clsFileParser(fileInfo, vbTab)
      End If

      dsLogData.Clear()

      While fileParser.peek() <> -1
         If (fileParser.readNextLine(singleRead)) Then
            If (singleRead.Length = dsLogData.Columns.Count) Then
               dsLogData.Rows.Add(singleRead)
            End If
         End If
      End While
   End Sub

I can't understand it, it's called and operates fine for the first 10 iterations and on the 11th throws and error. There is nothing special about that line in the log file.
 

nathani

New member
Joined
Aug 20, 2009
Messages
4
Programming Experience
1-3
I found the source of the disposal. Trying to determine the number of columns in the tab separated file by using this statement:
VB.NET:
         Using newStreamReader As StreamReader = New StreamReader(fStream)
            getColumnCount = Split(newStreamReader.ReadLine(), cDelim).Length
         End Using
fstream is what I use to open the StreamReader fStreamReader. By invoking the using statement with fStream it appearantly closes the fStream after it's done with it, which causes fStreamReader's baseobject to be closed. This causes the program to throw the ObjectDisposalException.
 
Top Bottom