How to Read a CSV File with Native .NET Functionality
Question:
How can I read a CSV file using C# without relying on third-party components?
Answer:
The .NET Framework provides the Microsoft.VisualBasic.FileIO.TextFieldParser class for parsing CSV files. To use this class, import the Microsoft.VisualBasic assembly.
Here's how to use the TextFieldParser to read a CSV file:
using Microsoft.VisualBasic.FileIO; var parser = new TextFieldParser(file); parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(new string[] { ";" }); while (!parser.EndOfData) { string[] row = parser.ReadFields(); // Process the current row }
The ReadFields method returns an array of strings representing the values of the current row in the CSV file.
The above is the detailed content of How to Read a CSV File Using Only Native .NET?. For more information, please follow other related articles on the PHP Chinese website!