How to filter Directory.GetFiles based on a dynamic list of extensions

This is how you can make Directory.GetFiles have multiple extension filters and also make that set of filters dynamic.

Assign myExtensions = “.xlsx,.txt”
Assign myPath = “C:\Temp”
Then use that in this expression:

System.IO.Directory.GetFiles(myPath).Where(Function(s) Split(myExtensions,",").Contains(System.IO.Path.GetExtension(s)))

Now you can dynamically provide the list of extensions you want, as a comma delimited string.


1 Like