Modified slightly from this example: https://stackoverflow.com/a/3527717/1920035
Private Function GetExcelFiles(ByVal directoryInfo As IO.DirectoryInfo) As IO.FileInfo()
If (directoryInfo Is Nothing) Then
Throw New ArgumentNullException(NameOf(directoryInfo))
End If
Dim files As IEnumerable(Of IO.FileInfo) = directoryInfo.GetFiles()
Dim excelFileExtensions() As String = {".xlsx", ".xlsm", ".xlsb", ".xltm", ".xlam", ".xls", ".xla", ".xlb", ".xlc", ".xld", ".xlk", ".xll", ".xlm", ".xlt", ".xlv", ".xlw"}
return files.Where(Function(file) excelFileExtensions.Contains(file.Extension))
End Function
What this does is:
- Get all files from the directory info
- Declare a collection of file extensions to check against
- Return only the files where the extension exists in the file extensions collection
Important to note - It may be worthwhile moving the excelFileExtensions so that it is a private readonly variable at a higher scope. It isn't much, but if you're running this a lot then it will make a difference.