Here I am going to write a simple program to read a text file line by line. We will use the ReadLine method of the StreamReader class.
C# Program to read text file line by line:
This program reads the content of the text file into a string variable using the ReadLine method of StreamReader class.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ReadFile { class Program { static void Main(string[] args) { int iCounter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader objFile = new System.IO.StreamReader(@"E:\SampleFile.txt"); while ((line = objFile.ReadLine()) != null) { System.Console.WriteLine(line); iCounter++; } objFile.Close(); System.Console.WriteLine("Total Line Read {0}", iCounter); System.Console.ReadLine(); } } }
View More:
- C# Program to Print Pyramid Pattern in C#.
- C# Program to Print Fibonacci Series.
- C# Program to check whether a number is prime or not using Recursion.
- C# Program to Swap two numbers without using third variable.
Conclusion:
I hope this is a useful topic for you. Your feedback and suggestions would always welcome to me.
Thank You.