In C#, File.ReadAllText() opens a text file, reads everything in it into a single string, and closes the file again. It is the easiest way to load a small file.
The default encoding is UTF-8, and the method also detects a byte order mark (BOM) for UTF-16 and UTF-32. Line breaks stay in the string exactly as they are in the file.
This is a static method of System.IO.File. Call it as File.ReadAllText(...); there is no new File(). For an object that remembers one file, see FileInfo.
Syntax
Pass the path. Add an Encoding when the file is not UTF-8.
Returns the whole file as a string, decoded as UTF-8 (or by BOM).
File.ReadAllText(path, Encoding.ASCII)
Decodes with the encoding you name.
File.ReadAllText(path).Split('\n')
Cuts the text into pieces. Use ReadAllLines when you really want lines.
ReadAllText loads the whole file into memory in one go. For a very large file use File.ReadLines or a StreamReader instead.
Library declarations
C#
public static string ReadAllText(string path);
public static string ReadAllText(string path, Encoding encoding);
Parameters
Parameter
Description
path
The file to read. It must exist.
encoding
Optional. The Encoding used to decode the bytes. Defaults to UTF-8, with BOM detection.
Return Value
Return Value
Description
string
The full content of the file. An empty file gives an empty string.
C# Examples
Example 1 — Read a Whole File into a String
Save a short message, read it back, and check the length.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-readalltext");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "message.txt");
File.WriteAllText(path, "Hello, file!");
string text = File.ReadAllText(path);
Console.WriteLine(text);
Console.WriteLine(text.Length);
Directory.Delete(root, true);
}
}
Output
Hello, file!
12
How It Works
A known file.WriteAllText saves Hello, file!, so the example does not depend on files on your computer.
One call, one string.ReadAllText opens the file, reads every character, closes the file, and returns the text.
Count the characters. The message has twelve characters, including the comma, the space, and the exclamation mark, so Length prints 12.
Example 2 — Split the Text into Lines Yourself
Read a file whose lines use \n and split the string on that character.
C#
using System;
using System.IO;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-readalltext-split");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string path = Path.Combine(root, "list.txt");
File.WriteAllText(path, "line1\nline2\nline3");
string text = File.ReadAllText(path);
string[] parts = text.Split('\n');
Console.WriteLine(parts.Length);
Console.WriteLine(parts[1]);
Console.WriteLine(text.Contains("line3"));
Directory.Delete(root, true);
}
}
Output
3
line2
True
How It Works
Explicit line breaks. The saved text uses \n between lines, so the result is the same on every system.
Line breaks stay in the string.ReadAllText does not remove them. Split('\n') cuts the string at each one and returns three parts.
Search the whole text.Contains("line3") works on the single string and prints True. Files written on Windows may use \r\n, so ReadAllLines is safer for real data.
Example 3 — Read UTF-8 and UTF-16 Files the Same Way
Save the same word with two encodings and see that ReadAllText decodes both.
C#
using System;
using System.IO;
using System.Text;
class Program {
static void Main() {
string root = Path.Combine(Path.GetTempPath(), "codetofun-file-readalltext-enc");
if (Directory.Exists(root)) Directory.Delete(root, true);
Directory.CreateDirectory(root);
string utf8 = Path.Combine(root, "utf8.txt");
string utf16 = Path.Combine(root, "utf16.txt");
File.WriteAllText(utf8, "cafe");
File.WriteAllText(utf16, "cafe", Encoding.Unicode);
Console.WriteLine(File.ReadAllBytes(utf8).Length);
Console.WriteLine(File.ReadAllBytes(utf16).Length);
Console.WriteLine(File.ReadAllText(utf8) == File.ReadAllText(utf16));
Directory.Delete(root, true);
}
}
Output
4
10
True
How It Works
Two encodings, one word. The UTF-8 file stores cafe in four bytes. The UTF-16 file uses two bytes per letter plus a two byte BOM at the start.
Count the bytes.ReadAllBytes shows 4 for UTF-8 and 10 for UTF-16, which is eight letter bytes plus two BOM bytes.
The BOM guides decoding.ReadAllText sees the BOM in the second file and switches to UTF-16 by itself, so both reads give the same string and the comparison prints True.
Frequently Asked Questions
The whole content of the file as one string. An empty file gives an empty string, and line breaks inside the file are kept in the text.
UTF-8 by default. It also detects UTF-16 and UTF-32 files that start with a byte order mark. Pass an Encoding as the second argument for other cases.
ReadAllText returns one string including line breaks. ReadAllLines returns a string array with one element per line and no line breaks.
Not really, because the whole file is loaded into memory. For big files, use File.ReadLines or a StreamReader and process the text in pieces.
FileNotFoundException. A missing folder throws DirectoryNotFoundException. Check File.Exists first or catch the exception.