VB.NET Language in a Nutshell

   
Directory.GetDirectories Method

Class

System.IO.Directory

Syntax

Directory.GetDirectories( path [, searchpattern ])

path (required; String)

A valid path to a directory

searchpattern (optional; String)

A directory specification, including wildcard characters

Return Value

An array of strings, each element of which is the name of a subdirectory

Description

Returns the names of the subdirectories in a particular directory

Rules at a Glance

Example

The following code displays all subdirectories of c:\ whose names start with the letter P:

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

Programming Tips and Gotchas

Since GetDirectories can return an empty array, you can prevent an array access error in either of two ways: you can iterate the returned array using the For Each...Next construct, or you can retrieve the value of the UBound function, which is -1 in the case of an uninitialized array.

See Also

Directory.GetFiles Method, Directory.GetFileSystemEntries Method

   

Категории