获取文本文件的日期创建

时间:2014-01-29 09:09:12

标签: c# date-format

我在c#中编写了一段代码来获取文本文件的创建日期并以yyyyMMddHHMMSS格式显示日期:

FileInfo fileInfo = new FileInfo("test.txt");
DateTime fileCreatedDate = fileInfo.CreationTime;
string dateCreation = fileCreatedDate.ToString("yyyyMMddHHMMSS");

但它不起作用,dateCreation例如:201401291001SS

我该如何修改?

5 个答案:

答案 0 :(得分:10)

您有大写分钟,即月份,请改为使用mm(和ss):

string dateCreation = fileCreatedDate.ToString("yyyyMMddHHmmss");

答案 1 :(得分:1)

试试这个

MM指的是两位数的月份

mm指两位数分钟

FileInfo fileInfo = new FileInfo("test.txt");
DateTime fileCreatedDate = fileInfo.CreationTime;
string dateCreation = fileCreatedDate.ToString("yyyyMMddHHmmss");

答案 2 :(得分:1)

看看这一行:

FileInfo fileInfo = new FileInfo("test.txt");

我想你忘了添加你的路径。请参阅以下示例:

 string path = @"yourpath\test.txt";
 FileInfo fileInfo = new FileInfo(path);

然后:

string dateCreation = fileCreatedDate.ToString("yyyyMMddHHmmss");

答案 3 :(得分:1)

//Take a file 
DateTime creationTime = File.GetCreationTime(@"C:\CC.txt");
//Print
Response.Write(creationTime.ToString("yyyyMMddHHmmss")); 

答案 4 :(得分:0)

FileInfo fileInfo = new FileInfo("test.txt");
DateTime fileCreatedDate = fileInfo.CreationTime;
string date = fileCreatedDate.ToString("yyyyMMddHHmmss");