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 StreamWriter objects in a using statement so they get closed properly.
using (StreamWriter oFile = File.AppendText("baza.txt"))
{
string output = "Current date and time: "
+ DateTime.Now.ToString("yyyy.MM.dd hh:mm:ss");
oFile.WriteLine(output);
}
Alternatively, you can manually call the Close method on the StreamWriter, but the using statement, to me, is much easier and less error-prone.
Its creating empty file because you are writing in it but not closing the StreamWriter
like this oFile.Close();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With