file name

  • Thread starter Thread starter microsoft.news.com
  • Start date Start date
M

microsoft.news.com

I have an app were i'm creating log files, but i want to give the log files
a distinctative names such as
Error_1/1/200512:30AM.log

how can I get the date/time in the file name? I tried this and it errors
out on me.

string strFileName = \\servername\\Error + "_" + System.DateTime.Now +
".log";

and it breaks, how can i get this working?
 
You cannot have such special characters in file names like "/, :, and
others". Just try renaming a file in Windows Explorer with any of these and
you will see that it won't. You will need to replace those characters with
another, maybe an underscore.

Alex
 
You're missing the conversion of DateTime to String

It should read:

string strFileName = "\\servername\\Error" + "_" +
System.DateTime.Now.ToString() + ".log"
 
You need to specify the format that the DateTime converts to to make it
file system friendly.

string strFileName =
"Error_" + System.DateTime.Now.ToString("yyyyMMddHHmm") + ".log

Should give:
Error_200504131319.log
....when generated @ 1:19pm today

HTH,
Dan
 
Back
Top