Read some lines from a text file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JustRun
    New Member
    • Mar 2008
    • 127

    Read some lines from a text file

    Hi all

    I'm trying to read some lines from a text file, and don't know how to do this.
    Actually I could read the whole file using object of StramReader,
    but to read custom line I think that I should use BeginRead() and EndRead() of FileStream class, but I dont know how to use them. I dont understand the parameters of them.

    If any one could explain, I'll be grateful.
    Thanks
  • belial
    New Member
    • Feb 2009
    • 4

    #2
    try this:

    Code:
    string line;
    using (StreamReader reader = new StreamReader(@"C:\testFile.txt"))
    {
       // read the first line
      line = reader.ReadLine();
      // read second line
      line = reader.ReadLine();
      
      while (!reader.EndOfStream)
      {
          // read next and next line till end of file
          line = reader.ReadLine();
      }
    }

    Comment

    • JustRun
      New Member
      • Mar 2008
      • 127

      #3
      Yes, it works, :D I swear I tried this b4 and i didn't work at all , but any way thank you cause you let me try it again and it works now :)

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        One thing I have done is ReadToEnd() into a string and then .Split it around the newlines to get an array of the lines.

        Comment

        Working...