I'm trying to write a search program in C# which will search for a string in a large text file(5GB). I've done a simple code shown below but the search results are time consuming and can take about 30 mins to complete. This is how my code looks like-
public List<string> Search(string searchKey)
{
List<string> results = new List<string>();
StreamReader fileReader = new StreamReader("D:\Logs.txt");
while ((line = fileReader.ReadLine()) != null)
{
if (line.Contains(searchKey)
{
results.Add(line);
}
}
}
Although the code works, it runs very slowly and takes about 30 mins to complete. Can we do something to bring the search time under a minute?