如何根据关键字列出文件名?
Dim allFileNames as String() =
Directory.GetFiles("C:\PDFFiles", "*.PDF", SearchOption.AllDirectories)
Dim filtered As IEnumerable(Of String) = Where(Function(fileName) Path.GetFileNameWithoutExtension(fileName).Contains("*ts"))
答案 0 :(得分:0)
试试我的方法:
''' <summary>
''' Return the files matching the specified file extension
''' and matching the specified keyword in the filename.
''' </summary>
''' <param name="Folder">Indicates the directory to search files.</param>
''' <param name="Recursivity">Indicates the subdirectory recursivity mode.</param>
''' <param name="FileExt">Indicates the file extension to match.</param>
''' <param name="KeyWord">Indicates a keyword to match on the filenames.</param>
''' <param name="IgnoreCase">Indicates the CaseSensitivity of the KeyWord match.</param>
''' <returns>System.Collections.Generic.List(Of System.IO.FileInfo).</returns>
Private Function GetFiles(ByVal Folder As String,
ByVal Recursivity As Boolean,
ByVal FileExt As String,
ByVal KeyWord As String,
Optional ByVal IgnoreCase As Boolean = False) As List(Of IO.FileInfo)
Return (
From File As String
In IO.Directory.GetFiles(Folder, "*" & FileExt, If(Recursivity, 1, 0))
Where If(IgnoreCase,
IO.Path.GetFileNameWithoutExtension(File).ToLower.Contains(KeyWord.ToLower),
IO.Path.GetFileNameWithoutExtension(File).Contains(KeyWord))
Select New IO.FileInfo(File)
).ToList
End Function
用法示例:
Dim Files As List(Of IO.FileInfo) =
GetFiles(Folder:="C:\Windows\System32", Recursivity:=False,
FileExt:="dll", KeyWord:="win", IgnoreCase:=True)
For Each File As IO.FileInfo In Files
MessageBox.Show(File.Name)
MessageBox.Show(File.Length)
Next File
更新:
只是一个重载方法:
''' <summary>
''' Return the files matching the specified file extension
''' and matching the specified keyword in the filename.
''' </summary>
''' <param name="Folder">Indicates the directory to search files.</param>
''' <param name="Recursivity">Indicates the subdirectory recursivity mode.</param>
''' <param name="FileExts">Indicates the file extensions to match.</param>
''' <param name="KeyWord">Indicates a keyword to match on the filenames.</param>
''' <param name="IgnoreCase">Indicates the CaseSensitivity of the KeyWord match.</param>
''' <returns>System.Collections.Generic.List(Of System.IO.FileInfo).</returns>
Private Function GetFiles(ByVal Folder As String,
ByVal Recursivity As Boolean,
ByVal FileExts As String(),
ByVal KeyWord As String,
Optional ByVal IgnoreCase As Boolean = False) As List(Of IO.FileInfo)
' Set all the file extensions to lower-case.
FileExts = (
From FileExt As String In FileExts
Where Not String.IsNullOrEmpty(FileExt)
Select FileExt.ToLower()
Distinct
).ToArray
Return (
From File As String In IO.Directory.GetFiles(Folder, "*", If(Recursivity, 1, 0))
Where FileExts.Contains(IO.Path.GetExtension(File).Substring(1).ToLower)
Where If(IgnoreCase,
IO.Path.GetFileNameWithoutExtension(File).ToLower.Contains(KeyWord.ToLower),
IO.Path.GetFileNameWithoutExtension(File).Contains(KeyWord))
Select New IO.FileInfo(File)
).ToList
End Function
用法示例:
Dim Files As List(Of IO.FileInfo) =
GetFiles(Folder:="C:\Windows\System32", Recursivity:=False,
FileExts:={"dll", "exe"}, KeyWord:="sys", IgnoreCase:=True)