最後に、@Andrew Morton と @dotINSolution の変更のおかげで、関数の使用を改善するために 2 つのオーバーロードを追加した汎用関数です。
#Region " Get Files "
' [ Get Files Function ]
'
' Examples :
'
' For Each file In Get_Files("C:\Windows", False) : MsgBox(file.Name) : Next
'
' For Each file In Get_Files("C:\Windows", True, "dll") : MsgBox(file.Name) : Next
' For Each file In Get_Files("C:\Windows", True, ".dll") : MsgBox(file.Name) : Next
' For Each file In Get_Files("C:\Windows", True, "*.dll") : MsgBox(file.Name) : Next
'
' For Each file In Get_Files("C:\Windows", False, {"dll", "ini"}) : MsgBox(file.Name) : Next
' For Each file In Get_Files("C:\Windows", False, {".dll", ".ini"}) : MsgBox(file.Name) : Next
' For Each file In Get_Files("C:\Windows", False, {"*.dll", "*.ini"}) : MsgBox(file.Name) : Next
' Get Files {directory} {recursive}
Private Function Get_Files(ByVal directory As String, ByVal recursive As Boolean) As List(Of IO.FileInfo)
Dim searchOpt As IO.SearchOption = If(recursive, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly)
Return IO.Directory.GetFiles(directory, "*", searchOpt).Select(Function(p) New IO.FileInfo(p)).ToList
End Function
' Get Files {directory} {recursive} {ext}
Private Function Get_Files(ByVal directory As String, ByVal recursive As Boolean, ext As String) As List(Of IO.FileInfo)
If ext.StartsWith("*") Then
ext = ext.Substring(1, ext.Length - 1)
ElseIf Not ext = "*" AndAlso Not ext.StartsWith(".") Then
ext = ("." & ext)
ElseIf ext = "*" Then
ext = Nothing
End If
Dim searchOpt As IO.SearchOption = If(recursive, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly)
Return IO.Directory.GetFiles(directory, "*" & ext, searchOpt).Select(Function(p) New IO.FileInfo(p)).ToList
End Function
' Get Files {directory} {recursive} {exts()}
Private Function Get_Files(ByVal directory As String, ByVal recursive As Boolean, ParamArray exts() As String) As List(Of IO.FileInfo)
Dim FileExtensions(exts.Count) As String
Dim ExtCount As Int64 = 0
For Each ext In exts
If ext.StartsWith("*") Then
FileExtensions(ExtCount) = ext.Substring(1, ext.Length - 1)
ElseIf Not ext = "*" AndAlso Not ext.StartsWith(".") Then
FileExtensions(ExtCount) = ("." & ext)
ElseIf Not ext = "*" AndAlso ext.StartsWith(".") Then
FileExtensions(ExtCount) = ext
ElseIf ext = "*" Then
FileExtensions(ExtCount) = Nothing
End If
ExtCount += 1
Next
Dim searchOpt As IO.SearchOption = If(recursive, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly)
Dim filenameExtComparer As New FilenameExtensionComparer
Return IO.Directory.GetFiles(directory, "*.*", searchOpt).Where(Function(o) FileExtensions.Contains(IO.Path.GetExtension(o), filenameExtComparer)).Select(Function(p) New IO.FileInfo(p)).ToList
End Function
' FilenameExtensionComparer
Public Class FilenameExtensionComparer : Implements IEqualityComparer(Of String)
Public Function Equals1(s As String, t As String) As Boolean Implements IEqualityComparer(Of String).Equals
Return String.Compare(s, t, StringComparison.OrdinalIgnoreCase) = 0
End Function
Public Function GetHashCode1(s As String) As Integer Implements IEqualityComparer(Of String).GetHashCode
Return s.GetHashCode()
End Function
End Class
#End Region