1

I'm trying to read in several string lines using stream reader, however I want to extract just the 4th line that is sent back when I make my query and display it in the console. Is there a better way of doing it rather than reading each line and saving it to a string. It's fine now as there's only 5 lines max being sent back but this would be terrible if I were to receive 10+ lines back.

All the lines are just plain text. Each time I receive from the server the lines are different so I have nothing to use as an id for a line.

sr is simply my stream reader that has been initialised like this:

StreamReader sr = new StreamReader(client.GetStream());

Below is my current method:

                    string Line1 = sr.ReadLine();
                    string Line2 = sr.ReadLine();
                    string Line3 = sr.ReadLine();
                    string Line4 = sr.ReadLine();
                    Console.WriteLine(Line4);
DanMan
  • 11
  • 5
  • There is no way to read just 4th line using `StreamReader`. You have to read everything, your current way is fine, just don't assign return values to anything. – Sinatr Jul 18 '18 at 13:32
  • Possible duplicate of [C# How to skip number of lines while reading text file using Stream Reader?](https://stackoverflow.com/questions/4418319/c-sharp-how-to-skip-number-of-lines-while-reading-text-file-using-stream-reader) – Sinatr Jul 18 '18 at 13:35

1 Answers1

3

You don't need the other lines, just read and ignore them.

for (int i = 0; i < 3; i++)
    sr.ReadLine();
string text = sr.ReadLine();
Ctznkane525
  • 7,297
  • 3
  • 16
  • 40