The following program should open/create a file and write the current date to it’s end every time.
using System;
using System.IO;
using System.Text;
namespace roughDraft
{
class Program
{
public static void Main()
{
StreamWriter oFile = File.AppendText("baza.txt");
string output = "Current date and time: " + DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss");
oFile.WriteLine(output);
Console.WriteLine(output);
Console.ReadKey();
}
}
}
I don’t know why does it only create an empty file.
You should always put
StreamWriterobjects in ausingstatement so they get closed properly.Alternatively, you can manually call the
Closemethod on theStreamWriter, but theusingstatement, to me, is much easier and less error-prone.