Reading from a file.(Kept Adjacent to the .EXE file)
StreamReader reader = new StreamReader("Test.txt");
while (reader.EndOfStream == false)
{
string line = reader.ReadLine();
Console.WriteLine(line);
}
reader.Close();
The above code will open up the file test.txt and display every line in the file on the
console. The while loop will stop the program when the end of the file is reached.
using System;
using System.IO;
class FileWriteandReadDemo
{
public static void Main()
{
StreamWriter writer;
writer = new StreamWriter("test.txt");
writer.WriteLine("hello world");
writer.Close();
StreamReader reader = new StreamReader("Test.txt");
while (reader.EndOfStream == false)
{
string line = reader.ReadLine();
Console.WriteLine(line);
}
reader.Close();
}
}