Reading a text file using StreamReader

Download .net eBook

Example

string fullOrRelativePath = "testfile.txt";

string fileData;

using (var reader = new StreamReader(fullOrRelativePath))
{
    fileData = reader.ReadToEnd();
}

Note that this StreamReader constructor overload does some auto encoding detection, which may or may not conform to the actual encoding used in the file.

Please note that there are some convenience methods that read all text from file available on the System.IO.File class, namely File.ReadAllText(path) and File.ReadAllLines(path).

Stats

Contributors: 3
2016-11-02
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook