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
Solved

C#: Looping into a StreamReader using foreach

Posted on 2008-10-08
2
3,801 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
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

765 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