File.ReadAllLines() Method in C# with Examples

Last Updated : 27 Oct, 2026

File.ReadAllLines() method is used to read all lines from a text file and store them in a string array. It belongs to the System.IO namespace and provides a simple way to access and process file contents line by line.

  • It reads the entire file and returns its contents as a string[] array.
  • Each array element represents one line of the file, without its line-ending characters.
  • It automatically closes the file after reading and supports specifying character encoding.

Syntax

File.ReadAllLines(path);

  • Parameters: path: Specifies the path of the text file to read.
  • Return Value: Returns a string[] containing all lines of the file. If the file is empty, it returns an empty array.

Overloads of File.ReadAllLines() Method

The File.ReadAllLines() method provides two overloads for reading text files.

Overload

Description

ReadAllLines(String)

Reads all lines using the default encoding behavior.

ReadAllLines(String, Encoding)

Reads all lines using the specified character encoding.

1. File.ReadAllLines(String) Method

Reads all lines from the specified text file and returns them as a string array. It is commonly used when the file does not require an explicitly specified encoding.

Syntax:

public static string[] ReadAllLines(string path);

Example: Suppose we have a text file named file.txt containing the following content:

file

Program to reads and displays all the lines from this file.

C#
using System;
using System.IO;

class Geeks{
    static void Main(){
        
        // Specify the file path
        string path = "file.txt";

        // Read all lines from the file
        string[] lines = File.ReadAllLines(path);

        // Display each line
        foreach (string line in lines){
            Console.WriteLine(line);
        }
    }
}

Output:

GFG
Geeks
GeeksforGeeks

Explanation:

  • File.ReadAllLines("file.txt") reads the contents of the text file and stores each line in the lines array.
  • The foreach loop accesses each element of the array and displays it using Console.WriteLine().

Note: The file.txt file must be available at the specified path. For a relative path, .NET looks in the application's current working directory.

2. File.ReadAllLines(String, Encoding) Method

Reads all lines from a text file using a specified character encoding. It is useful when working with files that contain Unicode characters or require a particular encoding format.

Syntax:

public static string[] ReadAllLines(
string path,
Encoding encoding
);

Parameters:

  • path: Specifies the path of the file to read.
  • encoding: Specifies the character encoding used to decode the file contents.

Example: Suppose we have a text file named file.txt containing the following content:

file

Example: Program to creates a UTF-8 encoded file and reads its contents using File.ReadAllLines().

C#
using System;
using System.IO;
using System.Text;

class Geeks{
    static void Main(){
        
        string path = "file.txt";

        // Create a file with UTF-8 encoding
        File.WriteAllLines(path, new string[]{
            "GFG",
            "Geeks",
            "GeeksforGeeks"
        }, Encoding.UTF8);

        // Read the file using UTF-8 encoding
        string[] lines = File.ReadAllLines(
            path, Encoding.UTF8);

        // Display file contents
        foreach (string line in lines)
        {
            Console.WriteLine(line);
        }
    }
}

Output: 

GFG
Geeks
GeeksforGeeks

Explanation:

  • File.WriteAllLines() creates a text file using UTF-8 encoding.
  • File.ReadAllLines(path, Encoding.UTF8) reads the file using UTF-8 encoding and returns its contents as a string array.

Note: The overload without an explicit encoding also supports UTF-8 and attempts to detect certain encodings using a byte order mark (BOM).

How File.ReadAllLines() Works

The File.ReadAllLines() method opens a text file, reads its contents, and stores each line in a string array.

Text File -> File.ReadAllLines() -> string[] Array

The method performs the following operations:

  1. Opens the text file using the specified path.
  2. Reads all lines and removes their line-ending characters.
  3. Stores the lines as individual elements of a string array.
  4. Closes the file and returns the array.

Accessing a Specific Line from a File

Since File.ReadAllLines() returns a string array, we can access individual lines using their indexes.

C#
using System;
using System.IO;

class Geeks{
    static void Main()
    {
        string path = "numbers.txt";

        // Create a sample file
        File.WriteAllLines(path, new string[]
        {
            "10", "20", "30", "40"
        });

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

        // Access specific lines
        Console.WriteLine("First Line: " + lines[0]);
        Console.WriteLine("Third Line: " + lines[2]);
        Console.WriteLine("Total Lines: " + lines.Length);
    }
}

Output
First Line: 10
Third Line: 30
Total Lines: 4

Explanation:

  • Each line is stored at a separate array index, starting from 0.
  • The program retrieves the first and third lines using lines[0] and lines[2].
  • The Length property returns the total number of lines.

Handling Exceptions in File.ReadAllLines()

The File.ReadAllLines() method can throw exceptions if the file does not exist, the path is invalid, or the program lacks permission to access the file.

C#
using System;
using System.IO;

class Geeks{
    static void Main(){
        
        string path = "missing.txt";

        try{
            // Attempt to read the file
            string[] lines = File.ReadAllLines(path);

            foreach (string line in lines){
                Console.WriteLine(line);
            }
        }
        catch (FileNotFoundException){
            
            Console.WriteLine("File not found.");
        }
        catch (UnauthorizedAccessException){
            
            Console.WriteLine("Access to the file is denied.");
        }
        catch (IOException){
            
            Console.WriteLine("An error occurred while reading.");
        }
    }
}

Output
File not found.

Explanation:

  • The try block attempts to read the specified file.
  • If the file is missing, FileNotFoundException is caught.
  • Other catch blocks handle access restrictions and general I/O errors.

Common Exceptions

The following exceptions may occur when using File.ReadAllLines().

Exception

Cause

ArgumentNullException

The specified file path is null.

FileNotFoundException

The specified file does not exist.

DirectoryNotFoundException

The directory in the path cannot be found.

UnauthorizedAccessException

Access is denied or the path refers to a directory.

IOException

An error occurs during file access or reading.

PathTooLongException

The file path exceeds the supported length.

NotSupportedException

The path has an unsupported format.

Other exceptions, including ArgumentException and SecurityException, may occur depending on the .NET version and execution environment.

File.ReadAllLines() Vs File.ReadLines()

Both methods are used to read text files line by line, but they differ in how file contents are loaded into memory.

Feature

File.ReadAllLines()

File.ReadLines()

Return Type

string[]

IEnumerable<string>

Reading

Reads all lines before returning

Enumerates lines lazily

Memory Usage

Higher for large files

Generally lower

Index Access

Supports direct indexing

No direct indexing

Best Use

Small or moderate files

Large files or sequential processing

Comment

Explore