Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# can i create a dynamic file name with streamwriter?

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

Whats 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);
like image 697
Sinaesthetic Avatar asked Oct 14 '10 08:10

Sinaesthetic


1 Answers

Probably a better way of adding timestamp to your file name would be to convert your datetime to string using some format and append to your file name. One example is given below -

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

if you dont use format string, then there will be characters like '/' and ':' which are not supported for naming a file.

like image 99
Sachin Shanbhag Avatar answered Oct 01 '22 13:10

Sachin Shanbhag