Question:
I need to read the entire contents of a text file and put it into a string .
I usually do it like this:
using(var reader = new StreamReader("arquivo.txt"))
{
var strBuilder = new StringBuilder();
while (objReader.ReadLine() != null)
{
strBuilder.Append(objReader.ReadLine());
}
}
var texto = strBuilder.ToString();
Is there another way or method to do this in a simpler way?
Answer:
In the .NET Framework 2.0 and later, the File
class has a ReadAllText()
method that does just that.
The code above would look like this:
var texto = File.ReadAllText("arquivo.txt");