Generally speaking, a text file will only contain one format (for example, comma-delimited or fixed-field width), but the text file is likely to contain multiple formats, what should you do when you encounter this situation?
If your text file contains multiple formats, you should use the PeekChars method of the TextFieldParser object to obtain a specific number of characters from scratch to determine the format of the data row. Then tell the TextFieldParser object The format of the data row and read the data row. The PeekChars method returns only the specified number of characters and does not advance to the next line, by which the format of each data row is identified and the rows are read row by row, enough to parse text files that contain multiple formats and read them smoothly.
For example, suppose that we have a text file named "Rich Text file. txt" in the text folder of the application project, which is special in that it contains the following three formats:
• Data rows with fixed field widths of 5, 10, and-1 respectively.
• Data rows with fixed field widths of 6, 10, 17, and 1, respectively.
• Comma-delimited rows of data.
One feature of the data rows in these three formats is that the first characters of the data rows are CK, Pb, and SP, so we simply get the top two characters of the data row by the TextFieldParser object's PeekChars method, and then according to its value To set the TextFieldType property, and set the Delimiters property (or call the Setdelimiters method) or the FieldWidths property (or invoke the SetFieldWidths method), and use the ReadFields side method to read rows of data. Repeated use of this method to process each row of data, you can successfully parse and read the entire text file.