using System; using System.IO; class Program { static void Main(string[] args) { using (StreamReader reader = new StreamReader("file.txt")) { string line; while ((line = reader.ReadLine()) != null) { Console.WriteLine(line); } } } }
using System; using System.IO; class Program { static void Main(string[] args) { using (StreamReader reader = new StreamReader("file.txt")) { string text = reader.ReadToEnd(); Console.WriteLine(text); } } }This example uses the StreamReader class to read a file named "file.txt" as a whole. The ReadToEnd method reads the entire file into a string variable. The StreamReader class is part of the System.IO namespace, which is included in the .NET Framework and .NET Core libraries.