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
Release: 2025-01-03 20:24:40
Original
426 people have browsed it

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
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template