该方法 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
可靠的编程
以下条件可能会导致异常:
路径对于以下原因之一无效:它是一个长度为零的字符串,它只包含空格,它包含无效字符,或者它是设备路径(以 \\.\) 开头(ArgumentException)。
路径无效,因为它是
Nothing
(ArgumentNullException)。指定通配符的一个或多个为
Nothing
空字符串,或仅包含空格(ArgumentNullException)。directory
不存在 (DirectoryNotFoundException)。directory
指向现有文件 (IOException)。路径超过系统定义的最大长度(PathTooLongException)。
路径中的文件或文件夹名称包含冒号(:)或格式无效(NotSupportedException)。
用户缺少查看路径所需的权限(SecurityException)。
用户缺少必要的权限(UnauthorizedAccessException)。