Thursday, October 10, 2013

Read Text File Line by Line

Many probably just want to know quickly, how to open a text file and how to read from it. Therefore I want to publish about it today and, since here exactly the principle of the previous post is used, just post the code.
The following code reads the text file "newposts.txt" from my desktop and writes its content line by line in the string Content:

System.IO.StreamReader sr = new System.IO.StreamReader(@"C:\Users\Oliver\Desktop\newposts.txt");
string Content = "";
string line = "";
while ((line = sr.ReadLine()) != null) // read until no more lines are available
    Content += line + Environment.NewLine;

No comments:

Post a Comment