如何:在 Visual Basic 中使用特定模式查找子目录

该方法 GetDirectories 返回字符串的只读集合,表示目录中子目录的路径名称。 可以使用参数 wildCards 指定特定模式。 如果要在搜索中包含子目录的内容,请将 searchType 参数设置为 SearchOption.SearchAllSubDirectories

如果未找到与指定模式匹配的目录,则返回空集合。

查找具有特定模式的子目录

GetDirectories使用该方法,提供要搜索的目录的名称和路径。 以下示例返回目录结构中所有名称包含“Logs”一词的目录,并将它们添加到ListBox1

For Each foundDirectory As String In
       My.Computer.FileSystem.GetDirectories(
           My.Computer.FileSystem.SpecialDirectories.MyDocuments,
           FileIO.SearchOption.SearchTopLevelOnly,
           "*Logs*")

    ListBox1.Items.Add(foundDirectory)
Next

可靠的编程

以下条件可能会导致异常:

另请参阅