如何将时间戳作为文件名放入C#脚本中的输出txt文件 [英] How to put time stamp as a file name to a output txt file in C# script

查看:337
本文介绍了如何将时间戳作为文件名放入C#脚本中的输出txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将时间戳作为文件名添加到C#脚本中的输出txt文件



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





我想要时间戳而不是默认文件名 test

解决方案

尝试:

  string  fileName = Path.Combine(   @ C:\izaz  ,DateTime.ToString( yyyy-MM-dd HH-mm-ss  )+.txt );  

使用 DateTime.Now 获取当前时间戳,然后使用日期格式调用 .ToString 你想要的(例如, yyyy-MM-dd-HH-mm-ss 这是年 - 月 - 日 - 小时 - 分 - 秒)和把它放在你的文件名中。此外,如果您使用逐字字符串文字(使用 @ ),则不必转义反斜杠。

 System.IO.File.AppendAllText( string  .Format( @  C:\izaz \ {0} .txt,DateTime.Now.ToString(  yyyy-MM-dd-HH-mm-ss)),  text to追加); 


你可以试试

 System.IO.File.AppendAllText (@C:\\izaz\\+ DateTime.Now.ToString(dd_MM_yyyy_hh_mm_ss)+。txt,Envireonment.NewLine +Duration:+ s + Environment.NewLine); 


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"

解决方案

Try:

string fileName = Path.Combine("@"C:\izaz", DateTime.ToString("yyyy-MM-dd HH-mm-ss") + ".txt");


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.

System.IO.File.AppendAllText(string.Format(@"C:\izaz\{0}.txt", DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")), "text to append");


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);


这篇关于如何将时间戳作为文件名放入C#脚本中的输出txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆