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

3

81

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

Eileen answered 3/6, 2010 at 11:34 Comment(3)
@Multitudinous - 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.Unmarked
@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: 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?Multitudinous
Not to forget there is also File.ReadLines which is lazy and hence cool.Coordination
T
112

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.

Targe answered 3/6, 2010 at 11:38 Comment(0)
L
19

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.

Lymphoma answered 3/6, 2010 at 11:38 Comment(1)
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.Goodloe
P
6

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

Potful answered 3/6, 2010 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.