At the moment I am opening the .txt file twice, once to get the number of all the lines, second to add a line to a list as much as how much lines there are in the .txt file. Is there an easier/better way to do this?
This is my code:
List<StreamReader> lijst = new List<StreamReader>();
StreamReader something1 = new StreamReader("C:\\something123.txt");
StreamReader something2 = new StreamReader("C:\\something123.txt");
lijst.Add(something1);
lijst.Add(something2);
{
int l = 0;
while (lijst[1].ReadLine() != null)
{
l++;
downloadLinks.Add(lijst[1].ReadLine());
}
for (int i = 0; i < l; i++)
{
String line = lijst[0].ReadLine();
aList.Add(line);
}
}
Then you are working currently working too hard. You can use
File.ReadLines
, which yields anIEnumerable<string>
and pass that into aList<string>
: