Another exanple;
public List<string> GetFlaggedSection(string filepath, string startflag, string endflag)
{
using (System.IO.StreamReader thefile = new System.IO.StreamReader(filepath))
{
string currentline;
List<string> selectedlines = null;
bool startlineseen = false;
while ((currentline = thefile.ReadLine()) != null)
{
if (currentline.ToLower() == startflag)
{
selectedlines = new List<string>();
startlineseen = true;
}
else if (currentline.ToLower() == endflag)
{
break;
}
else if (startlineseen)
{
selectedlines.Add(currentline);
}
}
thefile.Close();
return selectedlines;
}
}
Note:
1) use a 'using statement ... if you are not familiar with that, look it up
2) for production code: implement checking the file path is valid; clean lines even more than just making them lower-case as shown here; anticipate possible format errors like more than one, or none, "startflag" and throw appropriate errors.
3) Because of the possible complexity of flaws in the source file: look at using a higher level file with innate structure, like JSON.
To use this in a Console App:
class Program
{
static void Main(string[] args)
{
string path = Console.ReadLine();
lines = GetFlaggedSection(path, "//gotostart", "//gotoend");
foreach (string line in lines)
{
Console.WriteLine(line);
}
Console.ReadKey();
}
static List<string> lines = new List<string>();
static List<string> GetFlaggedSection(string filepath, string startflag, string endflag)
}
}
'lines and 'GetFlaggedSection must be declared static because they are used in a static context.