In this post we explained how to write text to a file in C#. Today we will explain reading text from a file using the StreamReader class.
So create a file "text.txt" in the same directory where is the executable of the project. We will create a C# Console Application.
First, we must include this namespace, to use the StreamReader class
using System.IO;
Then write this code in the place where you want your program to read the file
StreamReader sr = new StreamReader("text.txt"); string line; while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); }
With this code we read the text written in the file "text.txt" line by line, and print it on the console.
Simple as that.
Other Related Posts
Enter your Name and Email below to subscribe and start 1 month free course now.

