c sharp

C# Read File Line by Line

We use file streamings, input, and output streaming in C sharp. To read data from the file present in the system, we need an output stream ofstream. For this purpose, we provide the text file in the source code and read the contents line by line. This article will elaborate on the approaches we use to read a file.

To implement the phenomenon of the read file line by line, we have used a simple example to elaborate on some built-in features of c sharp. To read a file, we need to first create a file with some text inside it, so in the Documents folder, we have created a sample file with the text of 4 lines. You can also use an already existing file; in that case, there is no need to create another file, but make sure that file is in a text format.

This file will be used in the source code to read it. After creating a file, we will write a C sharp code to read it. First, use two basic libraries to be included in the code. These header files help to execute the code. System and system.IO are used for input and output streaming. It is preferred in the code when you need to display contents on the console or get the console input that the user enters. Inside the main program, we will first declare a string data type variable to store the file’s name that we want to open and read.

String filetoread = @"a.txt";

The data inside the file is read character by character and line by line. So for this purpose, we need to declare an array variable. All the lines are stored inside the array, and through this array, the contents of the file will be displayed.

String[] lines = File.ReadAllLines(FileToRead);

This declaration includes a function used to read the lines of the file as this function is a built-in feature of streaming, so it is accessed through the file object. Inside the parameter of this function, we have sent the variable that contains the name of that file. Using this, each line of that file will be accessed through the function and stored in the array. The contents will be displayed through the console.writeline() function. The parameter of this function will contain a join that itself is a function having an environment object to join new lines of the content in the array. Join function is a feature of string data type so that it will be accessed through the string. The second argument is the array.

String.Join(Environment. NewLine, Lines))

Save the code, and then execute it in the Ubuntu terminal. To compile the C sharp code in Ubuntu, we need an MCS compiler. Later on, Mono will assist in executing the code.

$ Mcs file.cs

$ mono file.exe

You can see that when the code is executed, each line of the file will be accessed and displayed through a source code.

Example # 2

This example deals with the use of a streamReader() function instead of the ReadAllLine() function. So just like the previous example, we have again created a new text file to display all the contents of a file. The text file is shown below.

The source code will contain a string variable that will store the name or the file’s path that you want to load and read line by line. A streamReader is created dynamically by using the new keyword. An object for the sreamreader is created through which all the file lines will be displayed.

StreamReader ReaderObject = new StreamReader(FileToRead))

The parameter of the streamReader function will contain the variable name that contains the name of the file that is to be opened. We will use this creation of an object with the ‘using’ statement, and it acts as a loop. Inside it, we declared a variable ‘Line’ that string type variable is used to read a single line from the file as the object of the streamline reads the line from the file; it is stored in the Line string. Readerobject reads a single line and then stores it in the Line string, and then it is displayed on the console. This whole concept is done through the while loop.

While (( Line = ReaderObject.ReadLine () ) != null )

While loop checks the status, if the readline() function is not null, then print each line of the file on the console.

Example # 3

There is another technique used to read the contents of a file and also uses the streamReader() function for reading data inside the file. A separate function for this purpose is created. Inside the function, a sreamreader is created by an object inside the function parameter; we have used the file name.

streamReader sr = new streamReader("a.txt");

Sr.Basestream.seek (0, seekOrigin.Begin);

An extra special feature used in this source code is an identifier that specifies the point from which you want to start reading the file through the input stream. This is done through the streamreader object. We have to read the contents of the file from the start, so we have written 0 indexes. To start the reading input, the Begin keyword is used here.

A string variable is created to read the file through the readline() function. A while loop will start and keep executing until the last character of the file through the string object. Each line accessed is first displayed on the console through the writeline() function, then again read by the readline() and stored in the string, and then the loop continues to execute. After the loop is completed, the file is closed through the streamreader object.


Inside the main program, a new object of the readfile is created through which we will be made the function call.


Now execute the code; all the contents of the file are displayed line by line; also, the extra spaces present in the file are displayed, as the space is also counted as a character.

If we change the index of the character inside the file in the second case, from where we need to start the data to be read from the file, then the index will move to the number we specified in the code.

For instance, we have changed the number from 0 to 10, so the character at the 10th point will be the first character to be displayed, and all the words after that will be read and displayed on the console. At 10th, the character is ‘s’ of the word sharp.


File streaming is an important feature in C sharp programming language; it involves input and output streaming. Today according to the topic, we have discussed the output streaming of the file. Data of the file is displayed by providing the name of the text file in the source code. This includes two basic features of C sharp, one is the ReadAllLines() function and the second one is through the streamReader(). We create an object for the streamreader, and then through this object, the file is accessed, and the data is displayed on the console.

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.