Any difference between File.ReadAllText() and using a StreamReader to read file contents?

There are no differences if you are using the ReadToEnd() method. The difference is if you are using the ReadLine() method for large files as you are not loading the whole file into memory but rather allows you to process it in chunks.

So use File.ReadAllText() instead of ReadToEnd() as it makes your code shorter and more readable. It also takes care of properly disposing resources as you might forget doing with a StreamReader (as you did in your snippet).


Looking at the code within mscorlib, File.ReadAllText actually calls StreamReader.ReadToEnd internally!

[SecurityCritical]
private static string InternalReadAllText(string path, Encoding encoding, bool checkHost)
{
    string result;
    using (StreamReader streamReader = new StreamReader(path, encoding, true, StreamReader.DefaultBufferSize, checkHost))
    {
        result = streamReader.ReadToEnd();
    }
    return result;
}