using System.IO; // Create a new StreamReader object to read from a file using (StreamReader reader = new StreamReader("filename.txt")) { // Read contents of the file with StreamReader string contents = reader.ReadToEnd(); // Do something with the file contents } // StreamReader object is disposed of automatically when leaving using block
using System.IO; using System.Text; // Create a new StreamReader object with specified encoding using (StreamReader reader = new StreamReader("filename.txt", Encoding.UTF8)) { // Read contents of the file with StreamReader string contents = reader.ReadToEnd(); // Do something with the file contents } // StreamReader object is disposed of automatically when leaving using blockThe System.IO namespace is part of the .NET Class Library, which is included with the .NET Framework and .NET Core packages.