C # can create dynamic file name using streamwriter?

I tried to make a program write to a file that was named with a timestamp. Basically, keeping a timestamp for the string value, I wanted it to create a file based on this timestamp. For example, "Flight Manifest on 10/14/2010 1:38:29 AM.txt"

What is the right way to do this?

I tried something like this:

string timeStamp = DateTime.Now.ToString(), filePath = string.Format("Flight Manifest {0}", timeStamp);
MessageBox.Show(filePath);

StreamWriter outputFile = new StreamWriter(filePath);
+5
source share
1 answer

Probably the best way to add a timestamp to your file name is to convert your datetime to a string using some format and add to your file name. One example is below -

string datetimeString = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}.txt",DateTime.Now);

, '/' ':', .

+9

All Articles