Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reading Text file line by line

Reading Text file line by line

Scheduled Pinned Locked Moved C#
question
3 Posts 2 Posters 6 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DownBySpj
    wrote on last edited by
    #1

    I want to process a text file line by line. how can i loop through all the line in a text file one by one

    L 1 Reply Last reply
    0
    • D DownBySpj

      I want to process a text file line by line. how can i loop through all the line in a text file one by one

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      .NET 2.0:

      string[] lines = File.ReadAllLines(@"C:\myfile.txt");

      .NET 1.1 (directly copied from MSDN):

      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);
      }

      regards

      D 1 Reply Last reply
      0
      • L Lost User

        .NET 2.0:

        string[] lines = File.ReadAllLines(@"C:\myfile.txt");

        .NET 1.1 (directly copied from MSDN):

        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);
        }

        regards

        D Offline
        D Offline
        DownBySpj
        wrote on last edited by
        #3

        thanx for the tip.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups