1

I am creating multiple .txt files for my DataGridView and I wanted to add a creation date to them. *****EDIT***** I have changed my code to the following to see if it would output a time stamp:

private void button2_Click(object sender, EventArgs e)
{
    DateTime timenow = new DateTime();
    DateTime.Now.ToString("yyyyddhh");
    Console.WriteLine(timenow.ToString());

    string LPath = Path.Combine(path, Path.GetRandomFileName() + ".txt");

    using (StreamWriter objWriter = new StreamWriter(LPath, true))
    {
        string LContent = textName.Text + "," + textVehicle.Text + "," + textDurationH.Text + " " + textDurationM.Text + "," + textFreight.Text + "," + textWeight.Text + "," + textIncome.Text + "," + LPath;
        objWriter.Write(LContent);
        infoTabelle.Rows.Add(LContent.Split(','));
        objWriter.WriteLine();
    }
    replace();
}

But it just outputs 01.01.0001 00:00:00. What do I have to do? :/


I have tried using various DateTime commands but none have seemed to work for me. Any help is appreciated!

Blacktempel
  • 3,935
  • 3
  • 29
  • 53
tegabyte
  • 69
  • 9
  • `DateTime.Now.ToString("yyyyddhh");` is not doing anything. Did you mean to assign it to a variable? – Bobby Speirs Oct 09 '17 at 13:16
  • OH ok so thats me problem then. It gives output in the Console but its not correct. How would I need to rewrite my code in order to save it with that date-timestamp? – tegabyte Oct 09 '17 at 13:17

1 Answers1

0

Try

DateTime timenow = DateTime.Now;

instead of

DateTime timenow = new DateTime();

Then add it to your filename string.

TTT
  • 1,848
  • 2
  • 30
  • 60
  • I tried doing so, but it gives me a System.NotSupportedException Error now. – tegabyte Oct 09 '17 at 13:33
  • I mistype sorry. I edited the answer and removed the "new" from DateTime timenow = DateTime.Now; – TTT Oct 09 '17 at 13:35
  • Ok so that command without the new is giving me a correct time, but it gives me a System.NotSupportedException Error at this line: using (StreamWriter objWriter = new StreamWriter(LPath, true)) – tegabyte Oct 09 '17 at 13:41