Linq to file system to get last created file in each sub folders

后端 未结 1 367
时光说笑
时光说笑 2021-01-27 03:03

I have a folder/directory that contains some sub directories.

Only those sub folders contain files. I have to get the full path of last created files in each sub folder.

相关标签:
1条回答
  • 2021-01-27 03:07

    Something like this would work:

    DirectoryInfo di = new DirectoryInfo(@"C:\SomeFolder");
    
    var recentFiles = di.GetDirectories()
                        .Select(x=>x.EnumerateFiles()
                                    .OrderByDescending(f=> f.CreationTimeUtc)
                                    .FirstOrDefault())
                        .Where(x=> x!=null)
                        .Select(x=>x.FullName)
                        .ToList();
    

    One thing to be mindful of is the permissions you need to traverse some protected directories, this shouldn't be a problem for most cases though.

    0 讨论(0)
提交回复
热议问题