The following example reads a string in from an existing text file.
using System; using System.IO; public class TextFromFile { public static void Main(String[] args) { StreamReader sr = File.OpenText("MyFile.txt"); String input = sr.ReadLine(); Console.WriteLine (input); sr.Close(); } }
This code creates a stream reader that points to MyFile.txt through a call to File.OpenText.You can now read a line from the file by calling StreamReader.ReadLine.