It looks like you just need to change the logic slightly:
- When you find the start line, set your variable to true (and continue the loop).
- When you find the end line, set your variable to false (and continue the loop, or break the loop if you only expect to have one section to capture).
- If your variable is true, capture the line
For example:
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith(start_token))
{
// We found our start line, so set "correct section" variable to true
inCorrectSection = true;
continue;
}
if (line.StartsWith(end_token))
{
// We found our end line, so set "correct section" variable to false
inCorrectSection = false;
continue; // Change this to 'break' if you don't expect to capture more sections
}
if (inCorrectSection)
{
// We're in the correct section, so capture this line
myList.Add(line);
}
}
1
solved Fetching lines between two different line of StreamReader in a List in c#