The simplest way to read a file is to use a high-level method in the .NET File API (in System.IO), such as File.ReadAllText(). These high-level methods abstract away the details of opening a file stream, reading it with StreamReader, and closing the file.
Here’s an example of reading a text file’s content using File.ReadAllText():
using System.IO;
var content = File.ReadAllText("hello.txt");
//Using absolute path
content = File.ReadAllText(@"C:\temp\hello.txt");
Console.WriteLine(content);
Code language: C# (cs)
Notice that you can use a relative or absolute file path when reading file.
This outputs the file’s contents:
Code language: plaintext (plaintext)Hello world! Blah blah etc etc Well, bye.
In this article, I’ll explain how to read files in different scenarios.
Table of Contents
Read a text file all at once
If the file is small, then you can read it into memory all at once. You can either read all of the text into a single string with File.ReadAllText(), or read the lines into a string array with File.ReadAllLines().
Read a text file into a string
To read a text file into a string, use File.ReadAllText(), like this:
using System.IO;
var content = File.ReadAllText(@"C:\temp\animals.txt");
Console.WriteLine(content);
Code language: C# (cs)
This outputs the file’s contents:
Code language: plaintext (plaintext)blue dog red cat white fish
If you’d benefit from reading the file asynchronously in your situation, you can use File.ReadAllTextAsync(). Here’s an example of asynchronously reading the text of a file into a string asynchronously:
using System.IO;
var content = await File.ReadAllTextAsync(@"C:\temp\animals.txt");
Code language: C# (cs)
Read all lines in a text file into an array
To load the text file into a string array, where each line is a separate string in the array, use File.ReadAllLines() like this:
using System.IO;
string[] animals = File.ReadAllLines(@"C:\temp\animals.txt");
//Process the array
foreach (var line in animals)
{
Console.WriteLine(line);
}
Code language: C# (cs)
Note: The async version is File.ReadAllLinesAsync().
This outputs the file’s lines (from the string array):
Code language: plaintext (plaintext)blue dog red cat white fish
Reading the file lines into an array would be useful if you need random access to specific lines later on (i.e. line 0 is lines[0]). That should be pretty rare. More likely, you’ll want to process one line at a time. In that case, use File.ReadLines() instead. I’ll show that next.
Read a text file line by line
Use File.ReadLines() when you want to read and process a text file one line at a time. This is a memory-efficient approach that reads a small amount of the file into memory at a time (buffering) and gives you one line at a time. Use this approach if you’re reading a big file (to avoid OutOfMemoryException) or if you’re doing a forward-only read.
Here’s an example of using File.ReadLines() to read and process a file line by line:
using System.IO;
foreach (var line in File.ReadLines(@"C:\temp\animals.txt"))
{
Console.WriteLine(line);
}
Code language: C# (cs)
This outputs the file’s lines:
Code language: plaintext (plaintext)blue dog red cat white fish
Read lines asynchronously with ReadLinesAsync() (.NET 7+)
If you want to read lines asynchronously, you can use ReadLinesAsync(). This was added in .NET 7. It returns an IAsyncEnumerable. Here’s an example of how to read a file with ReadLinesAsync():
using System.IO;
await foreach (var line in File.ReadLinesAsync(@"C:\temp\animals.txt"))
{
Console.WriteLine(line);
}
Code language: C# (cs)
This outputs the file’s lines:
Code language: plaintext (plaintext)blue dog red cat white fish