Directory.GetDirectories Method


Directory.GetDirectories Method

Class

System.IO.Directory

Syntax

     Dim result(  ) As String = Directory.GetDirectories(path[, _        searchPattern[, searchOption]]) 


path (required; String)

A valid path to a directory.


searchPattern (optional; String)

A directory specification, optionally including the wildcard characters * and ?.


searchOption (optional; SearchOption enumeration)

New in 2005. One of the following Microsoft.VisualBasic.FileIO.SearchOption enumeration members:

Member

Description

AllDirectories

The method returns information about all nested subdirectories.

TopDirectoryOnly

The method returns information about the topmost directory only.


If omitted, TopDirectoryOnly is used.

Description

The GetDirectories method returns an array of strings, with each string containing the name of one subdirectory of the specified main directory, optionally matching a pattern. The results can optionally include all nested subdirectories.

Usage at a Glance

  • path can be either an absolute path or a relative path from the current directory.

  • path can be either a path on the local system, the path of a mapped network drive, or a UNC path.

  • path cannot contain wildcard characters, but searchPattern can.

  • If searchPattern is specified, the method returns only those directories with names that match the string, which can contain wildcard characters. Otherwise, GetDirectories returns the names of all the subdirectories in the path directory.

  • If the directory specified by path has no subdirectories, or if no directories match searchPattern, an empty array is returned.

Example

The following code displays all top-level subdirectories of c:\ with names that start with the letter P:

     Dim thePDirectories(  ) As String     Dim counter As Integer     thePDirectories = Directory.GetDirectories("c:\", "P*")     For counter = 0 To UBound(thePDirectories)        Console.WriteLine(thePDirectories(counter))     Next counter 

Version Differences

  • The searchOption parameter is new with Visual Basic 2005.

  • Visual Basic 2005 includes a My.Computer.FileSystem.GetDirectories method that provides similar functionality.

See Also

Directory.GetFiles Method, Directory.GetFileSystemEntries Method




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net