Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hi,
I need a code to read all text from second line of "d.txt".

Help Me.
Posted
Comments
Maarten Kools 2-Sep-13 11:17am    
Did you try Google?[^]

There are a number of ways you could do this, but since this has a heavy smell of homework about it, I won't give you any code!

The first way is to use File.ReadAllLines[^] and then select the second line of the string array it returns, if there is one.

The other is to create a StreamReader, then read each line sequentially in a loop using StreamReader.ReadLine.aspx[^] until you get to the line number you want.

The first is quick, both to code and to execute, but uses a huge amount of memory for large files. The second is slower to code and to execute, but uses a small fraction of the memory space. Your choice!
 
Share this answer
 
Google is your friend so click me!

So above is a link to a simple google search, the phrase of which is:

read second line of text file c#

If you look at the results you will see that 5th link actually links directly a Code Project article.

I suggest you try reading some of those links.
 
Share this answer
 
Comments
Pouya Mozafar 2-Sep-13 11:46am    
I'm sorry next time I will be search google.....
C#
try
      {
          using (StreamReader sr = new StreamReader("d.txt"))
          {
              String line = sr.ReadToEnd();
              Console.WriteLine(line);
          }
      }
      catch (Exception e)
      {
          Console.WriteLine("The file could not be read:");
          Console.WriteLine(e.Message);
      }
 
Share this answer
 
Comments
ridoy 2-Sep-13 12:52pm    
Op wants to read 2nd line not all the text.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900