
File.ReadAllText reads the entire contents of a TXT file. The Microsoft .NET Framework provides this useful method in the System.IO namespace for this purpose. This method can be used instead of StreamReader in certain program contexts.
Key points: Use .NET Framework methods to read TXT files. This requires the System.IO namespace. The operation requires only one statement.

The .NET Framework provides utility methods on the File class that are easy to use for reading TXT files into your program. You do not need to loop or check an EOF constant. The requirements are that you reference the System.IO namespace or provide a fully qualified name such as "System.IO.File.ReadAllText". The next code defines the Main entry point and reads in text into the two strings.
Program that reads TXT files [C#]
using System;
using System.IO;
class Program
{
static void Main()
{
string value1 = File.ReadAllText("C:\\file.txt");
string value2 = File.ReadAllText(@"C:\file.txt");
Console.WriteLine("--- Contents of file.txt: ---");
Console.WriteLine(value1);
Console.WriteLine("--- Contents of file.txt: ---");
Console.WriteLine(value2);
}
}
Output
--- Contents of file.txt: ---
(Text)
--- Contents of file.txt: ---
(Text)Using System.IO namespace. Near the top of the program text, the System.IO namespace is included with the using directive. This gives you immediate access to the File class and its useful methods such as ReadAllText. Additionally, the program includes the System namespace so it can write the file contents to the screen.

File static methods. The two string references in the Main entry point are assigned to the string references returned by File.ReadAllText. The path is specified as an absolute path in the two method calls. The file the methods access is located on your C:\ drive and is named "file.txt". If you do not have that file on your disk, the methods will throw System.IO.FileNotFoundException.
FileNotFoundException Tips
Here we note some of what is actually being done in the C# program above. The File.ReadAllText method calls into other methods in the .NET Framework. At some point, low-level functions in Windows read in all the text using buffered reads. The text data is then put into an object with string character data.
Method result. The File.ReadAllText method then returns a pointer (reference) to this object data. The assignment to the result of File.ReadAllText then performs a simple and fast bitwise copy so the variables point to the object data.

Here we want to resolve whether File.ReadAllText is efficient. To answer this, the ReadAllText method was benchmarked against StreamReader. The result was that on a 4 KB file it was almost 40% slower.
Program that uses ReadAllText and StreamReader [C#]
using System.IO;
class Program
{
static void Main()
{
// Read in file with File class.
string text1 = File.ReadAllText("file.txt");
// Alternative: use custom StreamReader method.
string text2 = FileTools.ReadFileString("file.txt");
}
}
public static class FileTools
{
public static string ReadFileString(string path)
{
// Use StreamReader to consume the entire text file.
using (StreamReader reader = new StreamReader(path))
{
return reader.ReadToEnd();
}
}
}
Benchmark results
File.ReadAllText: 155 ms
FileTools.ReadFileString: 109 msStreamReader helper. In some projects, it would be worthwhile to use the above ReadFileString custom static method. In a project that opens hundreds of small files, it would save 0.1 milliseconds per file.

This article showed some basics regarding reading in text (TXT) files. However, you will likely need to do more advanced processing of these files at some point. Fortunately, there are many useful resources on how you can read separate lines, copy, delete and append to files, and replace files with new string data.
Path Examples Regex File Tutorial
We looked at how you can read text files into your C# program and use the contents as a string. We discussed some of the mechanisms the .NET Framework uses to manage text files and return the object data to your program. Finally, we explored some other tasks related to text (TXT) files in your C# programs.
File Handling