如何将时间戳作为文件名放入C#脚本中的输出txt文件 [英] How to put time stamp as a file name to a output txt file in C# script
本文介绍了如何将时间戳作为文件名放入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");
UseDateTime.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 isyear-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屋!
查看全文