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:

  • The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\) (ArgumentException).

  • The path is not valid because it is Nothing (ArgumentNullException).

  • One or more of the specified wildcard characters is Nothing, an empty string, or contains only spaces (ArgumentNullException).

  • directory does not exist (DirectoryNotFoundException).

  • directory points to an existing file (IOException).

  • The path exceeds the system-defined maximum length (PathTooLongException).

  • A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).

  • The user lacks necessary permissions to view the path (SecurityException).

  • The user lacks necessary permissions (UnauthorizedAccessException).

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 wordLogsin 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
Microsoft.VisualBasic.FileIO.FileSystem.GetDirectories