c sharp

C# Read CSV

When a program is dismissed, all data connected with it is deleted. As a result, we’ll need to save the data somewhere. Data is saved and shared indefinitely using files. Data contained in text files can be retrieved and modified using C#. In this article, we will read the data from a CSV file format created in Ubuntu text editor in c# programming language. For this, we have to know what is CSV file in C#.

What is a CSV File in C# in Ubuntu 20.04?

CSV (Comma Separated Values) is a frequently used data import and export layout for spreadsheets and databases. A data record is signified by each line in a CSV file. While CSV is a pretty simple file format, there can be a lot of variations, such as various delimiters, new lines, and quotation characters.

Structure of a CSV File in C# in Ubuntu 20.04

The arrangement of a CSV file is relatively basic. It’s a comma-delimited list of information. They can be much more complex, with thousands of lines, many entries per line, and long strings of text. Nearly CSV files don’t have headers at all, while more or less they utilize quote marks to enfold each chunk of data, but that’s the simple pattern.

The simplicity is an asset. CSV files are created to make data export and import simple. The generated data is easily understandable by humans and may be seen in a text editor.

Benefits of CSV File in C# in Ubuntu 20.04

  • CSV files are easy to manage and edit, and modifications are not secured until a user restricts a particular set of fields from updating. The best part is that, unlike PDF spreadsheet data downloads, CSVs can be modified afterward.
  • CSV imports are used by a variety of commercial software packages. CSVs are the predominant report output for several programs.
  • CSV files can be edited and created with Microsoft Excel or Google Sheets, and they’re widely used in spreadsheet apps.

How to Read the Content From a CSV File in the C# in Ubuntu 20.04?

In C#, the file class includes two static methods for reading CSV files, ReadAllText() and ReadAllLines().The ReadAllText() function returns a string encompassing the entire file’s text. This string must be saved in a variable and then used to display the data on the screen. On the other hand, ReadAllText()reads each line at a time from a file and returns it as a string. To store each line, we’ll need a string array. The same string array is used to present the data file. We have two programs of read CSV file in C#, and one of the programs read and write the CSV file content simultaneously.

Example # 1: Program to Read the CSV File Content in C# in Ubuntu 20.04

To deal with files in C#, the StreamReader class is utilized. It opens, reads, and assists with a variety of file functions. In addition, we can use this class to perform various actions on a CSV file. The CSV file is created separately as a “file.csv.” The CSV extension is used to create this file. After that, we have given a file path in the program.

We have C# library files at the top section of the program. The first file is a system.io namespace containing classes, structures, delegates, and enumerations related to IO. We can use these classes to read and write data to and from files and data streams. There are also classes for working with files and directories. The second one is the System.Collections.Generics defines interfaces and classes that describe generic collections, allowing users to design statically typed collections that are more type-safe and performant than non-generic statically typed collections. And the last library system library is used in our program, as indicated by the using System statement, which includes classes and functions such as the Console class and the WriteLine function/method.

After this, we established a class with the name “program,” and within the class, we have the main public function. Now, in the block of programs main, we have given a file path in a variable “filepath” of type string. The StreamReader class object is created as “Reader” and initialized with the null field. The if condition will verify the given CSV file path exists by using the Exists clause and reading the content of the file by defining the StreamReader.The OpenRead opens the file, and ReadLine is utilized to read the file content. Then, within the while loop, the EndOfStream property is called, which reads the entire stream from the current place to the end. It produces a string containing the remainder of the stream from the current point to the end; It produces an empty string if the current place in the stream is at the end. Note that whenever the compiler reads the coma in our CSV file, whenever the compiler reads the coma, it will split the line there. As we have called, the split function with the comma passed as a parameter inside the variable “values.”

To run this program, we have saved it as a “read.cs” file extension and then run it with the prompt shell “csc read.cs”. Then, using the command “mono read.exe,” the content of the file will be read in the prompt shell of Ubuntu as follows.

Example # 2: Program to Read and Write the CSV File Content Simultaneously in C# in Ubuntu 20.04

We can write the data in the CSV file, and at the same time, we can read the data in the CSV file. In the preceding example, we have read the file data with the read methods. Now, with the write method, we are going to write data in the CSV file.

First of all, we have defined the class as “Program” with the main function declaration. Then, we have stored the file path within the variable “file.” The data that needed to be written in the CSV format was placed in the variable “content.” The WriteAllText function is used to write the entire data all at once. It accepts two arguments: the file’s path and the text to be written. The console writeLine expression will display the current data of the CSV file. To write the data within the variable “ContentLines1” line by line, we have a WriteAllLines function. It comprises two arguments: the file’s path and the text to be written (a string array). Then, we have another approach, StreamWriter, which is used to write a single line of data of “ContentLines2” in the CSV file.

The data we wrote in the above source code and then read that data of the CSV file is as follows.

Conclusion

The discussion about reading the CSV files in C# has come to an end. We have a detailed discussion about CSV file, their structure, and their benefits in real applications. Then, we have two illustrations where we have implemented a code for reading the CSV files. Although, we have a program in which the read and write operation to the CSV file is possible in C#.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.