C#: Looping into a StreamReader using foreach

Is there any way I can loop inside a streamreader using foreach a get a String?
StreamReader sr = new StringReader(filePath);
foreach (Strins s in sr.ReadLine())
{
}

Open in new window

LVL 4
karakavAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Wayne Taylor (webtubbs)Connect With a Mentor AstronautCommented:
You'll need to use a "while" loop....

     while (sr.Peek != -1) {
         string s = sr.ReadLine();
     }

...or if you really must use a foreach, you can load the entire contents into a string, split that string by the newline char, then loop through each element of the resulting array....

    string st = sr.ReadToEnd;
    foreach (string s in st.Split(Environment.NewLine))
       {
    }

Wayne
0
 
karakavAuthor Commented:
Thanks.
0
All Courses

From novice to tech pro — start learning today.