C# Program to read all the lines one by one in a file

The File.ReadAllLines() method in C# reads all lines from a file and returns them as a string array. This method is useful when you need to process a file line by line or access specific lines by their index.

Syntax

Following is the syntax for File.ReadAllLines() method −

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

Parameters

  • filePath − The path to the file to be read (string)

Return Value

Returns a string[] array containing all lines from the file. Each element represents one line from the file.

Using ReadAllLines() to Read File Content

Let's say we have a file "sample.txt" with the following content −

One
Two
Three
Four

File.ReadAllLines() Process File Content One Two Three Four String Array lines[0] = "One" lines[1] = "Two" lines[2] = "Three" lines[3] = "Four" Each line becomes an array element Access lines using index: lines[0], lines[1], etc.

Example

using System;
using System.IO;

public class Demo {
   public static void Main() {
      // Create a sample file for demonstration
      string filePath = "sample.txt";
      string[] content = { "One", "Two", "Three", "Four" };
      File.WriteAllLines(filePath, content);
      
      // Read all lines from the file
      string[] lines = File.ReadAllLines(filePath);
      
      // Display lines one by one
      Console.WriteLine("Reading lines one by one:");
      Console.WriteLine("Line 1: " + lines[0]);
      Console.WriteLine("Line 2: " + lines[1]);
      Console.WriteLine("Line 3: " + lines[2]);
      Console.WriteLine("Line 4: " + lines[3]);
      
      // Clean up the file
      File.Delete(filePath);
   }
}

The output of the above code is −

Reading lines one by one:
Line 1: One
Line 2: Two
Line 3: Three
Line 4: Four

Using Loop to Read All Lines

Example

using System;
using System.IO;

public class Demo {
   public static void Main() {
      // Create a sample file
      string filePath = "data.txt";
      string[] content = { "First Line", "Second Line", "Third Line", "Fourth Line", "Fifth Line" };
      File.WriteAllLines(filePath, content);
      
      // Read all lines
      string[] lines = File.ReadAllLines(filePath);
      
      // Use loop to process each line
      Console.WriteLine("Total lines: " + lines.Length);
      Console.WriteLine("\nProcessing each line:");
      
      for (int i = 0; i < lines.Length; i++) {
         Console.WriteLine($"Line {i + 1}: {lines[i]}");
      }
      
      // Clean up
      File.Delete(filePath);
   }
}

The output of the above code is −

Total lines: 5

Processing each line:
Line 1: First Line
Line 2: Second Line
Line 3: Third Line
Line 4: Fourth Line
Line 5: Fifth Line

Using ReadAllLines() with Exception Handling

Example

using System;
using System.IO;

public class Demo {
   public static void Main() {
      string filePath = "test.txt";
      
      try {
         // Create sample file
         File.WriteAllText(filePath, "Line 1\nLine 2\nLine 3<br>");
         
         // Read all lines
         string[] lines = File.ReadAllLines(filePath);
         
         Console.WriteLine("File read successfully!");
         Console.WriteLine($"Number of lines: {lines.Length}");
         
         foreach (string line in lines) {
            Console.WriteLine($"Content: {line}");
         }
         
         File.Delete(filePath);
      }
      catch (FileNotFoundException) {
         Console.WriteLine("File not found!");
      }
      catch (Exception ex) {
         Console.WriteLine($"Error: {ex.Message}");
      }
   }
}

The output of the above code is −

File read successfully!
Number of lines: 3
Content: Line 1
Content: Line 2
Content: Line 3

Conclusion

The File.ReadAllLines() method provides an efficient way to read all lines from a file into a string array. It's particularly useful when you need random access to specific lines or want to process the entire file content at once. Always use exception handling when working with file operations to handle potential errors gracefully.

Updated on: 2026-03-17T07:04:35+05:30

457 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements