I'm new to c#. I'm trying to store the contents of a text file into a string. I tried the ReadAllLines method but it requires a string[]
Asked
Active
Viewed 926 times
4 Answers
3
Use File.ReadAllText() like this.
string result = File.ReadAllText(filename);
Yuval Itzchakov
- 141,979
- 28
- 246
- 306
recursive
- 80,919
- 32
- 145
- 234
1
Use the
File.ReadAllText()
You can find more information on the MSDN site
http://msdn.microsoft.com/en-us/library/system.io.file.readalltext(v=vs.110).aspx
CinnamonBun
- 1,150
- 14
- 28
0
You can use ReadAllText to read all the content from the text file into a single string.
shree.pat18
- 20,943
- 3
- 37
- 58
0
This will do what you seek:
using (StreamReader sr = new StreamReader("TextFile.txt"))
{
String line = sr.ReadToEnd();
Console.WriteLine(line);
}
LB2
- 4,773
- 17
- 34