I have a folder that could contain a mix of files. I want to find files in the folder that match a pattern e.g.
"template*.txt"
and return the most recently modified one.
I can see that I could use Directory.Getfiles to get the list, as it supports wildcards, and then loop through the list checking for the modified date of each file, but is there a better way to do this?
That will give you a
FileInfoobject orNothingif there is no matching file.EnumerateFilesis preferred overGetFilesunless you specifically want all the files before processing them. You should read the documentation for each to see what the specific difference is.