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

后端 未结 4 1049
伪装坚强ぢ
伪装坚强ぢ 2020-12-13 03:35

At first I used a StreamReader to read text from a file:

StreamReader reader = new StreamReader(dialog.OpenFile());
txtEditor.Text = reader.Read         


        
4条回答
  •  伪装坚强ぢ
    2020-12-13 04:26

    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;
    }
    

提交回复
热议问题