← Back to C# Course | Chapter 14: File I/O & Streams | Lesson 1 of 6

File.ReadAllText

In this page:

  1. File.ReadAllText

File.ReadAllText

File.ReadAllText reads an entire file into a single string in one call, which is the simplest way to load small-to-medium text files. File.WriteAllText is its counterpart for writing. Both are static methods on the System.IO.File class and open, use, and close the file automatically.

Note: For very large files, prefer StreamReader so you can process the file line by line instead of loading it all into memory.

Example: File.ReadAllText

markup
using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string path = "greeting.txt";
        File.WriteAllText(path, "Hello from File.WriteAllText!");

        string content = File.ReadAllText(path);
        Console.WriteLine(content);
    }
}
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.