Read Text File [C#]
These examples show how to read whole text file into string, how to read all lines into string array or how to read text file line by line to decrease memory usage.
The following examples require to add namespace using System.IO;
Read Text File into String
It's super easy to read whole text file into string using static class File and its method File.ReadAllText.
[C#]string text = File.ReadAllText(@"c:\file.txt", Encoding.UTF8);
Read Text File into String (with StreamReader)
Let's look under the hood of the previous example. Method File.ReadAllText is implemented similarly to the following code. The using statement ensures that method StreamReader.Dispose is called. The StreamReader's Dispose also calls FileStream.Dispose method which closes the file.
[C#]string text; var fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Read); using (var streamReader = new StreamReader(fileStream, Encoding.UTF8)) { text = streamReader.ReadToEnd(); }
Read Text File into String Array
Again, the easy way is to use static class File and it's method File.ReadAllLines.
[C#]string[] lines = File.ReadAllLines(@"c:\file.txt", Encoding.UTF8);
Read Text File into String Array (with StreamReader)
If you look under the hood of the File.ReadAllLines method, you can find implementation similar to this. As it was previously written, the using statement disposes StreamReader and FileStream (which closes the file).
[C#]string[] lines; var list = new List<string>(); var fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Read); using (var streamReader = new StreamReader(fileStream, Encoding.UTF8)) { string line; while ((line = streamReader.ReadLine()) != null) { list.Add(line); } } lines = list.ToArray();
Read Text File Line by Line
To reduce memory usage for large text files, you can process lines immediately instead of adding it to the list as in the previous example. To do that use File.ReadLines. This method internally creates Enumerator. Every time foreach asks for a next value, it calls StreamReader.ReadLine under the hood.
[C#]foreach (string line in File.ReadLines(@"c:\file.txt", Encoding.UTF8)) { // process the line }
Read Text File Line by Line (with StreamReader)
The code above can be implement also directly using StreamReader.ReadLine as shows the code bellow.
[C#]var fileStream = new FileStream(@"c:\file.txt", FileMode.Open, FileAccess.Read); using (var streamReader = new StreamReader(fileStream, Encoding.UTF8)) { string line; while ((line = streamReader.ReadLine()) != null) { // process the line } }
Files and Folders Examples
- [C#] FileStream Open File – how to open file using file stream
- [C#] FileStream Read File – how to safely read file stream
- [C#] Read Text File – how to read lines from text file
- [C#] Load Text File to String – how to load text from file to string
- [C#] Get Files from Directory – how to get files from directory
- [C#] Delete All Files – how to delete files from the specified directory
- [C#] Get Application Directory – how to get application or assembly folder
- [C#] File Attributes – how to get or set file attributes
- [C#] Get File Time – how to get last modification time of a file
- [C#] Open File With Associated Application – how to launch the default application
See also
- File – MSDN – static class to access files
- File.ReadAllText – MSDN – method to read whole file into string
- File.ReadAllLines – MSDN – method to read whole file into string array
- File.ReadLines – MSDN – method to read whole file line by line
- StreamReader.ReadLine – MSDN – method to read one text line from stream