我正在使用它来选择一个根文件夹并从每个目录中获取最新文件,然后再将此文件添加到我的文件listbox
中,但在某些情况下它不会选择最新文件。
var rootDirFile = Directory
.EnumerateFiles(pathToSearch, "*.doc", SearchOption.TopDirectoryOnly)
.OrderByDescending(f => File.GetLastWriteTime(f))
.Take(1);
var allNewestFilesOfEachFolder = Directory
.EnumerateDirectories(pathToSearch, "*.*", SearchOption.AllDirectories)
.Select(d => Directory.EnumerateFiles(d, "*.doc")
.OrderByDescending(f => File.GetLastWriteTime(f))
.FirstOrDefault());
foreach (string tempFile in rootDirFile.Concat(allNewestFilesOfEachFolder))
{
//add the file
}