Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C#: Looping into a StreamReader using foreach

Posted on 2008-10-08
2
Medium Priority
?
4,015 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 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview

824 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