Directory.GetFileSystemEntries Method

   
Directory.GetFileSystemEntries Method

Class

System.IO.Directory

Syntax

 Directory.GetFileSystemEntries(   path   [,   searchpattern   ]) 
path (required; String)

A valid path to a directory

searchpattern (optional; String)

A file specification, including wildcard characters

Return Value

An array of strings, each element of which contains the name of a filesystem entry (that is, a file or directory) in the path directory

Description

Returns the names of the filesystem entries (that is, of files and directories) in a specified directory

Rules at a Glance

  • path can be either an absolute path (a complete path from the root directory to the directory whose entries are to be retrieved) or a relative path (starting from the current directory to the directory whose entries are to be retrieved).

  • 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.

  • If searchpattern is specified, the method returns only those filesystem entries whose names match the string, which can contain wildcard characters. Otherwise, the function returns the names of all the filesystem entries in the target directory specified by path .

  • If the directory specified by path has no filesystem entries, or if no filesystem entries match searchpattern , an empty array is returned.

Example

The following code displays all filesystem entries in c:\ :

 Dim sEntries(  ) As String Dim i As Integer sEntries = Directory.GetFileSystemEntries("c:\") For i = 0 To UBound(sEntries)     Console.WriteLine(sEntries (i)) Next 

Programming Tips and Gotchas

  • The GetFileSystemEntries method combines the functionality of the GetDirectories and GetFiles methods .

  • Since GetFileSystemEntries 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.GetDirectories Method, Directory.GetFiles Method

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

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