Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to put time stamp as a file name to a output txt file in C# script

System.IO.File.AppendAllText(@"C:\\izaz\\test.txt",Envireonment.NewLine+"Duration: "+s+Environment.NewLine);


I want to have time stamp instead of default file name "test"
Posted
Updated 1-Nov-15 23:21pm
v3

Try:
C#
string fileName = Path.Combine("@"C:\izaz", DateTime.ToString("yyyy-MM-dd HH-mm-ss") + ".txt");
 
Share this answer
 
Use DateTime.Now to get the current timestamp, then call .ToString on it with the date format you want (for example, yyyy-MM-dd-HH-mm-ss which is year-month-day-hour-minute-second) and put that in your file name. Also, if you use a verbatim string literal (with @), you don't have to escape the backslashes.
C#
System.IO.File.AppendAllText(string.Format(@"C:\izaz\{0}.txt", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")), "text to append");
 
Share this answer
 
v4
You can try
System.IO.File.AppendAllText(@"C:\\izaz\\"+DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss")+".txt", Envireonment.NewLine + "Duration: " + s + Environment.NewLine);
 
Share this answer
 
You can do like this-
C#
string customFileName=@"C:\\izaz\\"+DateTime.Now.ToString("yyyyMMddHHmmssffff")+".txt";
System.IO.File.AppendAllText(customFileName,Envireonment.NewLine+"Duration: "+s+Environment.NewLine);

Hope, it helps :)
 
Share this answer
 
Comments
BillWoodruff 2-Nov-15 10:04am    
yyyyMMddHHmmssffff ... think about the effect of using this.
Suvendu Shekhar Giri 2-Nov-15 10:51am    
Sorry, not able to identify the issue. Could you please educate on this. Thanks in advance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900