TheDeveloperBlog.com


C# File.ReadAllText: TXT

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.

StreamReader

Info: Use .NET Framework methods to read TXT files. This requires the System.IO namespace. The operation requires only one statement.


Example. The .NET Framework provides utility methods on the File class. These are easy to use for reading TXT files into your program. You do not need to loop or check an EOF constant. Only one method call is needed.

Info: We must reference the System.IO namespace or provide a fully qualified name such as "System.IO.File.ReadAllText".

And: The next code defines the Main entry point and reads in text into the two strings.

C# program that reads TXT files

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, 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.

Also: The program includes the System namespace so it can write the file contents to the screen.

Using System

The two string references are assigned to the 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".

Caution: If that file does not exist, the methods will throw System.IO.FileNotFoundException.

FileNotFoundException

Internals. 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.

Result: The File.ReadAllText method then returns a pointer (reference) to this object data.

And: The assignment to the result of File.ReadAllText then performs a simple bitwise copy so the variables point to the object data.


Benchmark. 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.

Benchmark
C# program that uses ReadAllText and StreamReader

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 ms

StreamReader 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.


Discussion. 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. Many resources are available.

PathRegex File

Summary. 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.