78

What is the difference between File.ReadAllLines() and File.ReadAllText()?

iTayb
  • 12,373
  • 24
  • 81
  • 135
  • 11
    @AakashM - Have you tried reading the address bar? StackOverflow.com Q/A site? Its a perfectly fine question so either answer it or press the back button... sometimes 'offical' documentation doesn't provide clear clarity, thankfully we have sites like this where we can get the answers from a wide group of real world developers. – Dalbir Singh Jun 03 '10 at 12:41
  • @Dal : when the official documentation doesn't provide clarity, by all means let us ask questions about it; such as in this *good* question, also asked today: http://stackoverflow.com/questions/2966654 . The current question however includes no evidence that the asker has even *seen* the official documentation, hence my counter-question. Would "What does the + operator do in C#?" also be "perfectly fine", for you? – AakashM Jun 03 '10 at 19:49
  • Not to forget there is also `File.ReadLines` which is lazy and hence cool. – nawfal Dec 09 '13 at 16:23

3 Answers3

109

ReadAllLines returns an array of strings. Each string contains a single line of the file.

ReadAllText returns a single string containing all the lines of the file.

LukeH
  • 263,068
  • 57
  • 365
  • 409
18

File.ReadAllText() returns one big string containing all the content of the file while File.ReadAllLines() returns string array of lines in the file.

Keep in mind that in case of ReadAllText "The resulting string does not contain the terminating carriage return and/or line feed."

More details are available at remarks section of File.ReadAllText Method and File.ReadAllLines Method.

Giorgi
  • 30,270
  • 13
  • 89
  • 125
  • You've got your wires crossed here. `ReadAllText` will contain carriage returns and line feeds from the source... `ReadAllLines` will store each line in an array without the end of line characters. – Kevin Scharnhorst Nov 02 '20 at 18:09
6

ReadAllText reads it all in as one string, ReadAllLines reads it in as a StringArray.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116