In this article, I will talk about, how can we read a file line by line in C#. For this purpose, I will use StreamReader class present in .NET Framework.
- Start by adding System.IO namespace in your code.
- Now create a new instance of StreamReader class and pass the path of your file as a parameter in the Constructor.
StreamReader sr = new StreamReader(@"D:\HowToIdeas\text.txt");
- Now you can read a single line by using ReadLine() function present in StreamReader class.
string line = sr.ReadLine();
- Now read the text line by line until this ReadLine() function returns a null value.
Here is screen shot of sample code and its output.
Incoming search terms:
- read file using streamreader in c# (1)