开发者

how get the date and time of the last modified particular TYPE file in that directory in c#

how get the date and time of the last modified particular TYPE file in that directory

let me explain with an example

there are many files in directory say y:\tempfiles now i want to get the date and time of last modified file of txt format files like

9-03-2010 11.35 arun.reo 
9-03-2010 11.31 arun1.reo 
9-03-2010 11.31 arun.txt 
9-03-2010 11.31 arun.avi 

now i want the out put as 9-03-2010 11.35 which is last modifie开发者_运维知识库d file for reo type files .


var lastWrite = Directory.GetFiles(@"y:\tempfiles", "*.reo")
                         .Select(filename => File.GetLastWriteTime(filename))
                         .Max();
Console.WriteLine(lastWrite);


Use Directory.GetFiles Method to get all the txt files in the specified folder. Than you can use File.GetLastWriteTime Method to get the desired date.


An alternate LINQ version that I suspect will perform better than tzaman's, due to getting all the FileInfo entries at once, rather than doing a separate filesystem call for each filename:

var di = new DirectoryInfo("c:\\temp");
DateTime lastModified = di.GetFiles("*.dll")
                          .Select(fileInfo => fileInfo.LastWriteTime)
                          .Max();
Console.WriteLine(lastModified.ToString("dd-MM-yyyy HH.mm"));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜