Reading a text file using StreamReader

suggest change
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).

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents