DirectoryInfo dInfo = new DirectoryInfo(path)
DirectoryInfo[] directoryInfos = dInfo.GetDirectories();
如何迭代 directoryInfos 并选择最新的文件夹?
我正在寻找最新文件夹的名称
两种方法之一 - 不确定哪个是“最好的”:
var newest1 = directoryInfos.FirstOrDefault(di => di.CreationTime == directoryInfos.Max(d => d.CreationTime));
或者
var newest2 = directoryInfos.OrderByDescending(di => di.CreationTime).FirstOrDefault();
DateTime DD = directoryInfos.Max(x=> x.CreationTime);
string[] ss = directoryInfos.Where(x=> x.CreationTime == DD).Select(x=> x.Name).ToArray();
数组,因为可以有超过 1 个文件夹具有相同的最新创建时间
var folder = (from d in directoryInfos orderby d.CreationTime descending select d).First();
但其他答案当然更快:)