C# File.ReadAllText, Get String From File

Use the File.ReadAllText method from System.IO to get strings from entire files.
File.ReadAllText reads the entire contents of a text 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.File

System.IO: Near the top of the program, the System.IO namespace is included with the using directive.

Strings: The two string references are assigned to the references returned by File.ReadAllText.

Path: The path is specified as an absolute path in the 2 method calls. The file the methods access is located on your C:\ drive and is named "file.txt".

String Literal

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

FileNotFoundException
C# program that reads text 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)
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(); } } } Output 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. And we discussed some of the mechanisms the .NET Framework uses to manage TXT files and return the object data to your program.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls