C# File ReadAllLines() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

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.

C#
string path = Path.Combine(Path.GetTempPath(), "codetofun-readalllines.txt");
File.WriteAllLines(path, new[] { "one", "two", "three" });

string[] lines = File.ReadAllLines(path);

Console.WriteLine(lines.Length);
CallWhat it does
File.ReadAllLines(path)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

ParameterDescription
pathThe text file to read. It must exist.
encodingOptional. The Encoding used to decode the file. Defaults to UTF-8.

Return Value

Return ValueDescription
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);
    }
}

How It Works

  1. A trailing line break is ignored. The text ends with \n, but there is no fourth, empty line. The array has three elements.
  2. Index from zero. lines[1] is the second line, two.
  3. 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);
    }
}

How It Works

  1. 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.
  2. All are line breaks. ReadAllLines accepts each of them, so there are four lines.
  3. 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);
    }
}

How It Works

  1. 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.
  2. Filter with LINQ. Count(line => line.Length > 0) counts only the elements that have text. This needs using System.Linq;.
  3. 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.

Next: ReadAllText()

Read a whole text file into one string.

ReadAllText() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful