Reading Flat file in VB.NET

Below example will help you Reading flat file in VB.Net.

Using myReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\test.txt")
      myReader.TextFieldType = FileIO.FieldType.Delimited
      ' In this case data in flat file are separated by pipe("|").
      myReader.SetDelimiters("|")     
     
      Dim currentRow As String()
      While Not myReader.EndOfData
          Try
             currentRow = myReader.ReadFields()
             Dim currentField As String
             For Each currentField In currentRow
                 MsgBox(currentField)
             Next
          Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
          End Try
      End While
End Using

No comments:

Post a Comment