0
 DirectoryInfo dInfo = new DirectoryInfo(path)
 DirectoryInfo[] directoryInfos = dInfo.GetDirectories();

如何迭代 directoryInfos 并选择最新的文件夹?

我正在寻找最新文件夹的名称

4

3 回答 3

1

两种方法之一 - 不确定哪个是“最好的”:

var newest1 = directoryInfos.FirstOrDefault(di => di.CreationTime == directoryInfos.Max(d => d.CreationTime));

或者

var newest2 = directoryInfos.OrderByDescending(di => di.CreationTime).FirstOrDefault();
于 2012-04-23T10:22:25.693 回答
0
DateTime DD = directoryInfos.Max(x=> x.CreationTime);

string[] ss =  directoryInfos.Where(x=> x.CreationTime == DD).Select(x=> x.Name).ToArray();

数组,因为可以有超过 1 个文件夹具有相同的最新创建时间

于 2012-04-23T10:10:46.693 回答
0

var folder = (from d in directoryInfos orderby d.CreationTime descending select d).First();

但其他答案当然更快:)

于 2012-04-23T10:21:51.977 回答