In C#, File.ReadAllLinesAsync() opens a text file, reads all of its lines into a string[], and closes the file. It is the async version of File.ReadAllLines().
It returns a Task<string[]>. After you await it, every element of the array is one line, without the line break at the end.
Lines can end with \n, \r, or \r\n, so the method works with files from Windows, Linux, and macOS.
Syntax
Pass the path. Add an Encoding when the file is not UTF-8.
Stops with an OperationCanceledException if the token is cancelled.
The whole file is loaded into memory. For a big file, use File.ReadLinesAsync with await foreach to handle one line at a time.
Library declarations
C#
public static Task<string[]> ReadAllLinesAsync(string path, CancellationToken cancellationToken = default);
public static Task<string[]> ReadAllLinesAsync(string path, Encoding encoding, CancellationToken cancellationToken = default);
Parameters
Parameter
Description
path
The file to read. It must exist.
encoding
Optional. The Encoding used to decode the bytes. Defaults to UTF-8, with BOM detection.
cancellationToken
Optional. A token that lets you cancel the read before it finishes.
Return Value
Return Value
Description
Task<string[]>
A task that gives an array with one string per line, without the line breaks. An empty file gives an empty array.
C# Examples
Example 1 — Read a File into a String Array
Save three colors on three lines, read them back, and print each one.
C#
using System;
using System.IO;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-readalllinesasync");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "colors.txt");
await File.WriteAllTextAsync(path, "red\ngreen\nblue");
string[] lines = await File.ReadAllLinesAsync(path);
Console.WriteLine(lines.Length);
foreach (string line in lines) {
Console.WriteLine(line);
}
Directory.Delete(root, true);
}
}
Output
3
red
green
blue
How It Works
Three lines on disk. The saved text uses \n between the colors.
One element per line. After await, the array has three strings and no line breaks inside them.
Loop over the array.foreach prints red, green, and blue, one per line.
Example 2 — Mixed Line Endings and a Final Line Break
The method understands \r\n and \n in the same file, and ignores a final line break.
C#
using System;
using System.IO;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-readalllinesasync-endings");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string mixed = Path.Combine(root, "mixed.txt");
string trailing = Path.Combine(root, "trailing.txt");
await File.WriteAllTextAsync(mixed, "one\r\ntwo\nthree");
await File.WriteAllTextAsync(trailing, "a\nb\n");
string[] first = await File.ReadAllLinesAsync(mixed);
string[] second = await File.ReadAllLinesAsync(trailing);
Console.WriteLine(first.Length);
Console.WriteLine(string.Join(",", first));
Console.WriteLine(second.Length);
Directory.Delete(root, true);
}
}
Output
3
one,two,three
2
How It Works
Mixed endings. The first file uses \r\n after one and \n after two. Both count as line breaks.
Clean strings.string.Join(",", first) shows one,two,three with no stray \r characters.
The last break is ignored. The second file ends with a line break, yet there are only two lines, so Length prints 2.
Example 3 — Process Each Line: Add Up Numbers
Read a file with one number per line and add them together.
C#
using System;
using System.IO;
using System.Threading.Tasks;
class Program {
static async Task Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-readalllinesasync-sum");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "numbers.txt");
await File.WriteAllTextAsync(path, "10\n20\n30");
string[] lines = await File.ReadAllLinesAsync(path);
int total = 0;
foreach (string line in lines) {
total += int.Parse(line);
}
Console.WriteLine(lines.Length);
Console.WriteLine(total);
Directory.Delete(root, true);
}
}
Output
3
60
How It Works
Numbers as text. The file holds 10, 20, and 30 on separate lines. Every line is still a string.
Convert and add.int.Parse(line) turns each string into a number, and total collects the sum.
Print the result. Three lines were read and the sum is 60.
Frequently Asked Questions
A Task<string[]>. After you await it, you get a string array with one element per line of the file, without the line breaks.
ReadAllLinesAsync returns an array of lines. ReadAllTextAsync returns one string that still contains the line breaks.
ReadAllLinesAsync loads every line into an array first. ReadLinesAsync gives you one line at a time with await foreach, which uses less memory for big files.
Yes. It treats \n, \r, and \r\n as line breaks, so files from any system split into the same lines.