I am trying to read a particular field of a CSV file using ReadFields Method of TextFieldParser class. I will use the example at Microsoft to to show what I want to do.
https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser.readfields?view=netcore-3.1#Microsoft_VisualBasic_FileIO_TextFieldParser_ReadFields
From Microsoft's example, how do I get to retrieve, for example, only the second field of a 3-fields CSV file? :ehh:
https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser.readfields?view=netcore-3.1#Microsoft_VisualBasic_FileIO_TextFieldParser_ReadFields
Code:
Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\ParserText.txt")
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
MyReader.Delimiters = New String() {","}
Dim currentRow As String()
While Not MyReader.EndOfData
Try
currentRow = MyReader.ReadFields()
'How to read only one field instead of a row in this section?
For Each currentField As String In currentRow
My.Computer.FileSystem.WriteAllText(
"C://testfile.txt", currentField, True)
Next
Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
MsgBox("Line " & ex.Message & " is invalid. Skipping")
End Try
End While
End Using