I'm trying to read a very large file line by line and adding to a list, however when it arrives at a certain point I get an error: System.OutOfMemoryException
Is there any way to read this whole file in one go?
while ((line = trPlaces.ReadLine()) != null)
{
var htmlLine = line.Split('\t');
try
{
var newPlace = new Place
{
Id = htmlLine[0],
Name = htmlLine[2],
IsoCountry = htmlLine[8],
Latitude = htmlLine[4],
Longitude = htmlLine[5],
Admin1 = htmlLine[10].Length == 1 ? "0" + htmlLine[10] : htmlLine[10],
Admin2 = htmlLine[11],
PlaceType = htmlLine[7],
AlternativeNames = htmlLine[3].Split(',')
};
if(listPlaces1.Count< 4000000)
{
listPlaces1.Add(newPlace);
} else
{
listPlaces2.Add(newPlace);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}