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

后端 未结 4 1051
伪装坚强ぢ
伪装坚强ぢ 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:18

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

提交回复
热议问题