i_th i_th - 1 year ago 71
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 != "")



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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download