Using ReadLine method
using System.IO;
using System;
// Program to read every line in the file. ReadLine method of StreamReader class is used to read from file
class ReadFile
{
static void Main()
{
string fileloc="D://MyFile.txt"; // File location
// creates file
FileStream fs = new FileStream(fileloc, FileMode.Create);
fs.Close();
Console.WriteLine("File created successfully");
// writes to file
StreamWriter sw = new StreamWriter(fileloc);
sw.WriteLine("first line");
sw.WriteLine("second line");
sw.Flush();
sw.Close();
Console.WriteLine("Written to file successfully");
// Lets read the file now
Console.WriteLine("Content of file :");
StreamReader sr = new StreamReader(fileloc);
string text;
while ((text = sr.ReadLine()) != null)
{
Console.WriteLine(text);
}
Console.ReadLine();
}
}
Output:
File created successfully
Written to file successfully
Content of file:
first line
second line