Is there a way to specify the end of the extension?
There isn't a way to do this directly. The best option would be to switch to Directory.EnumerateFiles
and filter afterwards:
var files = Directory.EnumerateFiles(@"C:\Folder", "*.asp", SearchOption.AllDirectories)
.Where(f => f.EndsWith(".asp", StringComparison.OrdinalIgnoreCase));
This is because the Directory
methods have specific behavior which prevents this from working directly. From the docs:
If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. For example, "*.xls" returns both "book.xls" and "book.xlsx".
This is an exception to the normal search rules, but, in your case, is working against you. Using EnumerateFiles
streams the results, and filtering afterwards allows you to find only the proper matches.