Here is the code to read a text file from disk one line at a time into a string.  This code ensures the file exists and properly closes the file if an exception occurs.

using System;
using System.IO; 

namespace CSharp411
{
    class Program
    {
        static void Main( string[] args )
        {
            string filePath = @"c:\temp\test.txt";
            string line; 

            if (File.Exists( filePath ))
            {
                StreamReader file = null;
                try
                {
                    file = new StreamReader( filePath );
                    while ((line = file.ReadLine()) != null)
                    {
                        Console.WriteLine( line );
                    }
                }
                finally
                {
                    if (file != null)
                        file.Close();
                }
            } 

            Console.ReadLine();
        }
    }
}

Popularity: 43% [?]

Related posts:

  1. C# Read String Line by Line
  2. C# Read Text File into String
  3. C# Search/Replace in Files
  4. Read a Web Page in C#
  5. Check Valid File Path in C#