Reading a text file using StreamReader
suggest changestring 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents