Solved

C#: Looping into a StreamReader using foreach

Posted on 2008-10-08
2
3,849 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:karakav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
ID: 22667330
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
 
LVL 4

Author Closing Comment

by:karakav
ID: 31504157
Thanks.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question