i have to read 10 lines from the middle of a file, i tried to use seek method but it requires number of bytes. i dont know how many bytes will be contained in 10lines. how can i tell seek to read 10 lines from a specified location.

Recommended Answers

All 3 Replies

If the file you're trying to read is a textfile, then you can use the TextReader's ReadLine method.

Try this and see if it helps.

TextReader reader = New StreamReader("<file to read>");
string line = "";

int startReadingHere = <enter line number here>;
int lineIndex = 0;
int readLines = 0;

while (reader.Peek() != -1)
{
   if (lineIndex >= startReadingHere && readLines < 10)
   {
      line = reader.ReadLine();
      //Do something with the string
      readLines += 1;
   }
   lineIndex += 1;
}
commented: my thoughts exactly :) +1

If this has been helpful, please mark this thread as solved.

At 20 days old, i'm not sure it was worth resurrecting this thread to have it marked solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.