Solved

C#: Looping into a StreamReader using foreach

Posted on 2008-10-08
2
3,729 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
Comment Utility
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
Comment Utility
Thanks.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now