且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何创建文本文件.文件名应与上次创建的文件名不同.

更新时间:2021-11-15 19:52:16

1)您可以使用Guid作为文件名.

2)您可以保留一个计数器,并为每个保存的文件增加计数器.
1) You can use Guid as your file name.

2) You can keep a counter and increment that for each file saved.


尝试一下
string FileName=Test+DateTime.Now.ToString().Replace("/", "").Replace(".", "").Replace(":", "").Replace(" ", "")+".txt";
string text = "Test string to write inside the text file";
System.IO.File.WriteAllText(@FileName, text);


使用FileName + HH:MM:SS
连接文件 fileName024658

试试这个
Concatenate file with FileName + HH:MM:SS
fileName024658

Try this
string path = @"D:\";
string fileName = "TestFile" + DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace(" ", "") + ".txt";
File.Create(path + fileName);