This is a very simple way to read and write to a text file using TextReader and TextWriter.
First you need to include System.IO
Code:
using System.IO;
Reading a Text File:
Code:
static void Main(string[] args)
{
// create reader & open file
TextReader tr = new StreamReader("date.txt");
// read a line of text
Console.WriteLine(tr.ReadLine());
// close the stream
tr.Close();
}
Writting a Text File
Code:
static void Main(string[] args)
{
// create a writer and open the file
TextWriter tw = new StreamWriter("date.txt");
// write a line of text to the file
tw.WriteLine(DateTime.Now);
// close the stream
tw.Close();
}