Share via


My.Computer.FileSystem.GetDirectories Method

Returns a collection of strings representing the path names of subdirectories within a directory.

' Usage
Dim value As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetDirectories(directory)
Dim value As System.Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetDirectories(directory ,searchType ,wildcards)
' Declaration
Public Function GetDirectories( _
   ByVal directory As String _
) As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
' -or-
Public Function GetDirectories( _
   ByVal directory As String, _
   ByVal searchType As SearchOption, _
   ByVal wildcards As String() _
) As System.Collections.ObjectModel.ReadOnlyCollection(Of String)

Parameters

  • directory
    String. Name and path of directory. Required.

  • searchType
    SearchOption Enumeration. Whether to include subfolders. Default is SearchOption.SearchTopLevelOnly. Required.

  • wildcards
    String. Pattern to match names. Required.

Return Value

Read-only collection of String.

Exceptions

The following conditions may produce an exception:

Remarks

You can use the wildcards parameter to specify a specific pattern. If you would like to include the contents of subdirectories in the search, set the searchType parameter to SearchAllSubDirectories.

An empty collection is returned if no directories matching the specified pattern are found.

Tasks

The following table lists examples of tasks involving the My.Computer.FileSystem.GetDirectories method.

To

See

List subdirectories with a specific pattern

How to: Find Subdirectories with a Specific Pattern in Visual Basic

Example

The following example returns all the directories in the directory structure that contain the word Logs in their names and adds them to ListBox1.

For Each foundDirectory As String In _
My.Computer.FileSystem.GetDirectories _
(My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
True, "*Logs*")
    ListBox1.Items.Add(foundDirectory)
Next

This example requires that you have a ListBox named ListBox1 on your form.

Requirements

Namespace:Microsoft.VisualBasic.MyServices

Class:FileSystemProxy (provides access to FileSystem)

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

Permissions

The following permission may be necessary:

Permission

Description

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

For more information, see Code Access Security and Requesting Permissions.

See Also

Reference

My.Computer.FileSystem Object

SearchOption Enumeration

System.Collections.ObjectModel.ReadOnlyCollection<T>

FileSystem.GetDirectories