Home >Backend Development >C++ >How to Read a CSV File Using Only Native .NET?

How to Read a CSV File Using Only Native .NET?

Linda Hamilton
Linda HamiltonOriginal
2025-01-03 20:24:40463browse

How to Read a CSV File Using Only Native .NET?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn