Read a file line-by-line with C#

Google Translate Icon

This post will discuss how to read the contents of a file line by line in C#.

There are several ways to read the contents of a file line by line in C#. These are discussed below in detail:

1. Using File.ReadLines() method

The recommended solution to read a file line by line is to use the File.ReadLines() method, which optionally takes a specific character encoding. The following code example demonstrates its usage to read the lines of a file.

Download Code

 
The ReadLines throws an IOException if an I/O error occurs while opening the file and FileNotFoundException if the file is not found.

When working with huge files, ReadLines is very efficient as it returns an Enumerable, and we can start enumerating before the whole collection is returned.

2. Using File.ReadAllLines() method

We can also use the ReadAllLines() method to read a file line by line.

Unlike ReadLines, which returns an Enumerable, ReadAllLines returns a string array containing all lines of the file, and we must wait for the whole array of strings to be returned before accessing the array. Therefore, we should not use it with large files.

Like ReadLines, ReadAllLines optionally takes a specific character encoding and throws an IOException if an I/O error occurs while opening the specified file and FileNotFoundException if the source file is not found.

Download Code

 
The File class also offers the ReadAllText() method, which reads all the text in the file in one go. The following code example uses the ReadAllText() method to read all the text in the file in one operation and returns a string. To read the file line by line, split the string using String.Split() method with the newline as a delimiter.

Download Code

3. Using StreamReader.ReadLine() method

Another solution is to use the StreamReader.ReadLine() method. We can use it to read lines from a file until the end of the file is reached.

Download Code

 
We can also use the StreamReader.ReadToEnd() method, but it reads the whole file in a single operation.

The following code opens the text file using a stream reader, copies the contents to a string, then splits the string with newline as separator using the String.Split() method. Since the stream reader is declared and instantiated in a using statement, the Dispose() method is automatically called to flush and closes the stream.

Download Code

That’s all about reading a file line-by-line with C#.

Rate this post

Average rating 4.85/5. Vote count: 34

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Tell us how we can improve this post?




Thanks for reading.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding :)



Subscribe
Notify of
guest
3 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Do NOT follow this link or you will be banned from the site!