In C#, File.ReadAllLines() reads a text file and returns its lines as a string[]. Each element is one line, without the line break at the end.
It understands \r\n, \n, and \r as line breaks, so files from Windows, Linux, and old Mac systems all work. The whole file is loaded before the method returns.
This is a static method of System.IO.File. Call it as File.ReadAllLines(...); there is no new File(). For an object that remembers one file, see FileInfo.
Syntax
Read the lines, then use Length, indexes, or a foreach loop.
Returns all lines as a string[], decoded as UTF-8.
File.ReadAllLines(path, encoding)
Same, but decodes with the encoding you pass.
File.ReadAllLines(path)[0]
Reads the file and picks the first line. Only do this when the file has at least one line.
Because it returns an array, you get Length and random access like lines[2]. File.ReadLines returns a lazy IEnumerable<string> instead.
Library declarations
C#
public static string[] ReadAllLines(string path);
public static string[] ReadAllLines(string path, Encoding encoding);
Parameters
Parameter
Description
path
The text file to read. It must exist.
encoding
Optional. The Encoding used to decode the file. Defaults to UTF-8.
Return Value
Return Value
Description
string[]
One element per line, without line breaks. An empty file gives an empty array.
C# Examples
Example 1 — Read the Lines and Pick One
Save three lines, read them as an array, and use Length and an index.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-readalllines");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "numbers.txt");
File.WriteAllText(path, "one\ntwo\nthree\n");
string[] lines = File.ReadAllLines(path);
Console.WriteLine(lines.Length);
Console.WriteLine(lines[1]);
for (int i = 0; i < lines.Length; i++) {
Console.WriteLine((i + 1) + ". " + lines[i]);
}
Directory.Delete(root, true);
}
}
Output
3
two
1. one
2. two
3. three
How It Works
A trailing line break is ignored. The text ends with \n, but there is no fourth, empty line. The array has three elements.
Index from zero.lines[1] is the second line, two.
Number the lines. The loop adds one to the index to print human-friendly numbers from 1 to 3.
Example 2 — Different Line Endings, Same Result
Mix three kinds of line break in one file and see that all are understood.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-readalllines-endings");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "mixed.txt");
File.WriteAllText(path, "a\r\nb\nc\rd");
string[] lines = File.ReadAllLines(path);
Console.WriteLine(lines.Length);
Console.WriteLine(string.Join("|", lines));
Directory.Delete(root, true);
}
}
Output
4
a|b|c|d
How It Works
Three styles in one file. Between a and b is the Windows break \r\n, between b and c the Unix break \n, and between c and d the old Mac break \r.
All are line breaks.ReadAllLines accepts each of them, so there are four lines.
Join to show them.string.Join("|", lines) prints the four elements on one row. Splitting with Split('\n') instead would have left stray \r characters.
Example 3 — Count Lines That Are Not Blank
Read a file that has empty lines and use LINQ to count the real ones.
C#
using System;
using System.IO;
using System.Linq;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-readalllines-blank");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "gaps.txt");
File.WriteAllText(path, "x\n\ny\n\n");
string[] lines = File.ReadAllLines(path);
int filled = lines.Count(line => line.Length > 0);
Console.WriteLine(lines.Length);
Console.WriteLine(filled);
Directory.Delete(root, true);
}
}
Output
4
2
How It Works
Blank lines are real lines. The text x, an empty line, y, and another empty line gives four array elements. Two of them are empty strings.
Filter with LINQ.Count(line => line.Length > 0) counts only the elements that have text. This needs using System.Linq;.
Both numbers. The array has 4 elements and 2 of them hold text. Use string.IsNullOrWhiteSpace in the test if lines of only spaces should also be skipped.
Frequently Asked Questions
A string array with one element per line of the file. The line breaks themselves are not part of the elements.
Windows (\r\n), Unix (\n), and old Mac (\r) line endings are all treated as line breaks, even when they are mixed in one file.
ReadAllLines loads everything into an array before it returns. ReadLines returns lines lazily as you loop, which uses less memory for big files.
No. A single line break at the very end does not create an extra element. Blank lines in the middle do create empty strings.
Read the array, change the element (lines[2] = "new"), and call File.WriteAllLines(path, lines). The file is replaced with the new lines.