using System.IO; string filePath = @"C:\example.txt"; using (StreamReader streamReader = new StreamReader(filePath)) { string line = ""; while ((line = streamReader.ReadLine()) != null) { // Do something with the line } }
using System.IO; string filePath = @"C:\example.txt"; using (StreamReader streamReader = new StreamReader(filePath)) { string fileContent = streamReader.ReadToEnd(); // Do something with the file content }This example reads a file located at "C:\example.txt" and stores its contents in a string variable called "fileContent". The StreamReader class is a part of the System.IO package in the .NET framework.