0

Hi,
if someone can help me how to read a line in the txt document. I know you read the first line or the entire document but i do not know how a particular line...

3
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by Dum_
0

I would imagine that you need to read through the document one line at a time until you get to a certain one. Either know the line number (and use a loop to iterate through) or use a general expression to find the text, if you do not know the line number.

0

Yes, i know line number but i donćt know how to read only that line number...

1

From MSDN:

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            // Create an instance of StreamReader to read from a file.
            // The using statement also closes the StreamReader.
            using (StreamReader sr = new StreamReader("TestFile.txt")) 
            {
                string line;
                // Read and display lines from the file until the end of 
                // the file is reached.
                while ((line = sr.ReadLine()) != null) 
                {
                    Console.WriteLine(line);
                }
            }
        }
        catch (Exception e) 
        {
            // Let the user know what went wrong.
            Console.WriteLine("The file could not be read:");
            Console.WriteLine(e.Message);
        }
    }
}

You should be able to modify the while loop to go to the specific line number. Just execute sr.ReadLine() that many times until you get to your line.

Votes + Comments
Very good
1

Use something like this:

using (StreamReader sr = new StreamReader(path)) 
            {
                while (sr.Peek() >= 0) 
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
Votes + Comments
well
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.