i_th i_th - 3 days ago 6
C# Question

How can I read one line from more than one files very quickly C#

When all files is bigger than 60 Mb ,it works very slowly.

int count1 = File.ReadLines(sharesList.Items[0].ToString()).Count();

//The sharesList contains all files

for (int m = 0; m < count1; m++)
{
foreach (String s in sharesList.Items)
{
if (File.Exists(s))
{
string shar1 = File.ReadAllLines(s).Skip(m).Take(1).First();

if (shar1 != null && shar1 != "")
{
sharesL.Add(shar1);

}


}
}
}


Could anyone tell me how to make it very fast?
Thanks in Advance

Answer

Instead of using ReadAllLines, write a method that calls ReadLine() in a for loop iterating up to m, then close the file and return that line.

This will save time because you'll only read the file up to the line that you need.

Without actually reading the file, there is no way to seek to the correct location based on a count of lines.

Comments