Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Directory.GetFileSystemEntries Method (String, String)

Returns an array of file system entries matching the specified search criteria.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

'Declaration
Public Shared Function GetFileSystemEntries ( _
	path As String, _
	searchPattern As String _
) As String()
'Usage
Dim path As String
Dim searchPattern As String
Dim returnValue As String()

returnValue = Directory.GetFileSystemEntries(path, searchPattern)
public static String[] GetFileSystemEntries (
	String path, 
	String searchPattern
)
public static function GetFileSystemEntries (
	path : String, 
	searchPattern : String
) : String[]
Not applicable.

Parameters

path

The path to be searched.

searchPattern

The search string to match against the names of files in path. The searchPattern parameter cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any of the characters in InvalidPathChars.

Return Value

A String array of file system entries matching the search criteria.

Exception typeCondition

UnauthorizedAccessException

The caller does not have the required permission.

ArgumentException

path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

-or-

searchPattern does not contain a valid pattern.

ArgumentNullException

path or searchPattern is a null reference (Nothing in Visual Basic).

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.

IOException

path is a file name.

DirectoryNotFoundException

The specified path is invalid (for example, it is on an unmapped drive).

The following wildcard specifiers are permitted in searchPattern.

Wildcard character

Description

*

Zero or more characters.

?

Exactly one character.

Characters other than the wildcard specifiers represent themselves. For example, the searchPattern string "*t" searches for all names in path ending with the letter "t". The searchPattern string "s*" searches for all names in path beginning with the letter "s".

The matching behavior of searchPattern when the extension is exactly three characters long is different from when the extension is more than three characters long. A searchPattern of exactly three characters returns files having an extension of three or more characters. A searchPattern of one, two, or more than three characters returns only files having extensions of exactly that length.

The following list shows the behavior of different lengths for the searchPattern parameter:

  • "*.abc" returns files having an extension of.abc,.abcd,.abcde,.abcdef, and so on.

  • "*.abcd" returns only files having an extension of.abcd.

  • "*.abcde" returns only files having an extension of.abcde.

  • "*.abcdef" returns only files having an extension of.abcdef.

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

The path parameter is not case-sensitive.

For a list of common I/O tasks, see Common I/O Tasks.

The following example uses the GetFileSystemEntries method to fill an array of strings with the names of all files matching a user-specified filter in a specific location and prints each string in the array to the console. The example is configured to catch all errors common to this method.

Option Explicit On 
Option Strict On

Imports System

Namespace GetFileSystemEntries
    Class Class1
        Overloads Shared Sub Main(ByVal args() As String)
            Dim snippets As New Class1()
            Dim path As String = System.IO.Directory.GetCurrentDirectory()
            Dim filter As String = "*.exe"
            snippets.PrintFileSystemEntries(path)
            snippets.PrintFileSystemEntries(path, filter)
            snippets.GetLogicalDrives()
            snippets.GetParent(path)
            snippets.Move("C:\proof", "C:\Temp")
        End Sub 'Main

        Sub PrintFileSystemEntries(ByVal path As String)
            Try
                ' Obtain the file system entries in the directory path.
                Dim directoryEntries As String()
                directoryEntries = System.IO.Directory.GetFileSystemEntries(path)
                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub
        Sub PrintFileSystemEntries(ByVal path As String, _
                                   ByVal pattern As String)
            Try
                ' Obtain the file system entries in the directory
                ' path that match the pattern.
                Dim directoryEntries As String()
                directoryEntries = _
                   System.IO.Directory.GetFileSystemEntries(path, pattern)

                Dim str As String
                For Each str In directoryEntries
                    System.Console.WriteLine(str)
                Next str
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                        "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.DirectoryNotFoundException
                System.Console.WriteLine("The path encapsulated in the " + _
                                        "Directory object does not exist.")
            End Try
        End Sub

        ' Print out all logical drives on the system.
        Sub GetLogicalDrives()
            Try
                Dim drives As String()
                drives = System.IO.Directory.GetLogicalDrives()

                Dim str As String
                For Each str In drives
                    System.Console.WriteLine(str)
                Next str
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An I/O error occurs.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            End Try
        End Sub
        Sub GetParent(ByVal path As String)
            Try
                Dim directoryInfo As System.IO.DirectoryInfo
                directoryInfo = System.IO.Directory.GetParent(path)
                System.Console.WriteLine(directoryInfo.FullName)
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                     "contains only white spaces, or " + _
                                     "contains invalid characters.")
            End Try
        End Sub
        Sub Move(ByVal sourcePath As String, ByVal destinationPath As String)
            Try
                System.IO.Directory.Move(sourcePath, destinationPath)
                System.Console.WriteLine("The directory move is complete.")
            Catch exp As ArgumentNullException
                System.Console.WriteLine("Path is a null reference.")
            Catch exp As System.Security.SecurityException
                System.Console.WriteLine("The caller does not have the " + _
                                           "required permission.")
            Catch exp As ArgumentException
                System.Console.WriteLine("Path is an empty string, " + _
                                        "contains only white spaces, " + _
                                        "or contains invalid characters.")
            Catch exp As System.IO.IOException
                System.Console.WriteLine("An attempt was made to move a " + _
                                        "directory to a different " + _
                                        "volume, or destDirName " + _
                                        "already exists.")
            End Try
        End Sub
    End Class
End Namespace


package GetFileSystemEntries ;
import System.*;

class Class1
{
    public static void main(String[] args)
    {
        Class1 snippets = new Class1();

        String path = System.IO.Directory.GetCurrentDirectory();
        String filter = "*.exe";

        snippets.PrintFileSystemEntries(path);
        snippets.PrintFileSystemEntries(path, filter);
        snippets.GetLogicalDrives();
        snippets.GetParent(path);
        snippets.Move("C:\\proof", "C:\\Temp");
    } //main

    void PrintFileSystemEntries(String path)
    {
        try {
            // Obtain the file system entries in the directory path.
            String directoryEntries[] = 
                System.IO.Directory.GetFileSystemEntries(path);

            for (int iCtr = 0; iCtr < directoryEntries.length; iCtr++) {
                String str = directoryEntries[iCtr];
                System.Console.WriteLine(str);
            }
        }
        catch (ArgumentNullException exp) {
            System.Console.WriteLine("Path is a null reference.");
        }
        catch (System.Security.SecurityException exp) {
            System.Console.WriteLine(("The caller does not have the "
                + "required permission."));
        }
        catch (ArgumentException exp) {
            System.Console.WriteLine(("Path is an empty string, "
                + "contains only white spaces, "
                + "or contains invalid characters."));
        }
        catch (System.IO.DirectoryNotFoundException exp) {
            System.Console.WriteLine(("The path encapsulated in the "
                + "Directory object does not exist."));
        }
    } //PrintFileSystemEntries

    void PrintFileSystemEntries(String path, String pattern)
    {
        try {
            // Obtain the file system entries in the directory
            // path that match the pattern.
            String directoryEntries[] = 
                System.IO.Directory.GetFileSystemEntries(path, pattern);

            for (int iCtr = 0; iCtr < directoryEntries.length; iCtr++) {
                String str = directoryEntries[iCtr];
                System.Console.WriteLine(str);
            }
        }
        catch (ArgumentNullException exp) {
            System.Console.WriteLine("Path is a null reference.");
        }
        catch (System.Security.SecurityException exp) {
            System.Console.WriteLine(("The caller does not have the "
                + "required permission."));
        }
        catch (ArgumentException exp) {
            System.Console.WriteLine(("Path is an empty string, "
                + "contains only white spaces, "
                + "or contains invalid characters."));
        }
        catch (System.IO.DirectoryNotFoundException exp) {
            System.Console.WriteLine(("The path encapsulated in the "
                + "Directory object does not exist."));
        }
    } //PrintFileSystemEntries

    // Print out all logical drives on the system.
    void GetLogicalDrives()
    {
        try {
            String drives[] = System.IO.Directory.GetLogicalDrives();

            for (int iCtr = 0; iCtr < drives.length; iCtr++) {
                String str = drives[iCtr];
                System.Console.WriteLine(str);
            }
        }
        catch (System.IO.IOException exp) {
            System.Console.WriteLine("An I/O error occurs.");
        }
        catch (System.Security.SecurityException exp) {
            System.Console.WriteLine(("The caller does not have the "
                + "required permission."));
        }
    } //GetLogicalDrives

    void GetParent(String path)
    {
        try {
            System.IO.DirectoryInfo directoryInfo = 
                System.IO.Directory.GetParent(path);
            System.Console.WriteLine(directoryInfo.get_FullName());
        }
        catch (ArgumentNullException exp) {
            System.Console.WriteLine("Path is a null reference.");
        }
        catch (ArgumentException exp) {
            System.Console.WriteLine(("Path is an empty string, "
                +"contains only white spaces, or contains invalid characters."));
        }
    } //GetParent

    void Move(String sourcePath, String destinationPath)
    {
        try {
            System.IO.Directory.Move(sourcePath, destinationPath);
            System.Console.WriteLine("The directory move is complete.");
        }
        catch (ArgumentNullException exp) {
            System.Console.WriteLine("Path is a null reference.");
        }
        catch (System.Security.SecurityException exp) {
            System.Console.WriteLine(("The caller does not have the "
                + "required permission."));
        }
        catch (ArgumentException exp) {
            System.Console.WriteLine(("Path is an empty string, "
                +"contains only white spaces, or contains invalid characters."));
        }
        catch (System.IO.IOException exp) {
            System.Console.WriteLine(("An attempt was made to move a "
                + "directory to a different "
                + "volume, or destDirName "
                + "already exists."));
        }
    } //Move
} //Class1

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions

Show:
© 2014 Microsoft