Search code examples
c#fileinfogetfiles

Get a certain file extension while getting files based on creation date from a directory


Im trying to get all files from a certain directory based on the oldest creation date. Is there any way i can filter out all extensions that are not .tif? the code that im using is below.

string dir = KQV.Default.Directory;
DirectoryInfo info = new DirectoryInfo(dir);
FileInfo[] files = info.GetFiles().OrderByDescending(p => p.CreationTime).ToArray();

For some reason i cant seem to search with a where endswith

Edit: Endswith needs a field to work with, for this case the name of the file. Got it thanks to the replies here :3


Solution

  • Just simple use Where, so code looks like (tested):

    DirectoryInfo info = new DirectoryInfo(@"C:\tmp");
            FileInfo[] files = info.GetFiles()
                .Where(f=>!(f.FullName.EndsWith("tif")))
                .OrderByDescending(p => p.CreationTime)
                .ToArray();