
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
How to read a CSV file and store the values into an array in C#?
A CSV file is a comma-separated file, that is used to store data in an organized way. It usually stores data in tabular form. Most of the business organizations store their data in CSV files.
In C#, StreamReader class is used to deal with the files. It opens, reads and helps in performing other functions to different types of files. We can also perform different operations on a CSV file while using this class.
OpenRead() method is used to open a CSV file and ReadLine() method is used to read its contents.
OpenRead() method is used to open a CSV file and ReadLine() method is used to read
Data.csv A,B,C
Example
class Program{ public static void Main(){ string filePath = @"C:\Users\Koushik\Desktop\Questions\ConsoleApp\Data.csv"; StreamReader reader = null; if (File.Exists(filePath)){ reader = new StreamReader(File.OpenRead(filePath)); List<string> listA = new List<string>(); while (!reader.EndOfStream){ var line = reader.ReadLine(); var values = line.Split(','); foreach (var item in values){ listA.Add(item); } foreach (var coloumn1 in listA){ Console.WriteLine(coloumn1); } } } else { Console.WriteLine("File doesn't exist"); } Console.ReadLine(); } }
Output
A B C
- Related Articles
- How to read CSV file in Python?
- How to read data from .csv file in Java?
- How to read the data from a CSV file in Java?\n
- How to read a Pandas CSV file with no header?
- How to read data from *.CSV file using JavaScript?
- How to store list in a txt file and read list from txt file in android?
- How to read text file into a list or array with Python?
- How to store drawable resources ID in the form of R.drawable.* inside an array using an XML values file?
- What kind of settings can we do to a CSV file by query while exporting the values from MySQL table into a CSV file?
- How to append data into a CSV file using PowerShell?
- How to read a file into a string in Golang?
- Write a program in Python to store the city and state names that start with ‘k’ in a given DataFrame into a new CSV file
- How to use SharedPreferences on Android to store, read and edit values?
- How to read an input image and print it into an array in matplotlib?
- How to store array values in MongoDB?

Advertisements