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...

Recommended Answers

All 5 Replies

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.

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

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.

commented: Very good +8

Use something like this:

using (StreamReader sr = new StreamReader(path)) 
            {
                while (sr.Peek() >= 0) 
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
commented: well +8

Tnx ;).

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.