得到没有斜杠的日期

xbo*_*nez 15 c#

我需要在文件名中创建一个具有今天日期的文件.这是我正在使用的代码:

FileName = ConfigurationManager.AppSettings["OutputFileLocation"] + "\\" + DateTime.Now.Date.ToShortDateString() + ConfigurationManager.AppSettings["OutputFileName"];
Run Code Online (Sandbox Code Playgroud)

但由于日期以斜杠(2011/11/10)返回,因此最终会查找具有这些名称的目录.我怎样才能将日期定为20111110?

Red*_*ter 35

DateTime.Now.ToString("yyyyMMdd")
Run Code Online (Sandbox Code Playgroud)

此外,您可能没有意识到System.IO.Path.Combine.它使建筑路径更清洁,更加万无一失.


And*_*ins 5

这样的事情怎么样:

System.IO.FileInfo file = new System.IO.FileInfo(File1.PostedFile.FileName);
string fname = file.Name.Remove((file.Name.Length - file.Extension.Length));
fname = fname + Now.ToString("_MMddyyyy_HHmmss") + file.Extension;
Run Code Online (Sandbox Code Playgroud)

这将创建带有嵌入日期时间的文件名。然后,您可以在文件夹路径前添加路径并将其传递给SaveAs。