Reading a text file using StreamReader

suggest change
string fullOrRelativePath = "testfile.txt";

string fileData;

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

This StreamReader constructor detects a byte-order mark. If the file has no BOM, it decodes the bytes as UTF-8, which may not be the file's encoding.

File.ReadAllText(path) and File.ReadAllLines(path) also read a whole text file.

Feedback about page:

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



Table Of Contents