This documentation is archived and is not being maintained.

DirectoryInfo.GetFiles Method (String)

Returns a file list from the current directory matching the given searchPattern.

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

public FileInfo[] GetFiles(
	string searchPattern
)

Parameters

searchPattern
Type: System.String

The search string, such as "*.txt".

Return Value

Type: System.IO.FileInfo[]
An array of type FileInfo.

ExceptionCondition
ArgumentException

searchPattern contains invalid characters. To determine the invalid characters, use the GetInvalidPathChars method.

ArgumentNullException

searchPattern is null.

DirectoryNotFoundException

The path is invalid, such as being on an unmapped drive.

SecurityException

The caller does not have the required permission.

If there are no files in the DirectoryInfo, this method returns an empty array.

The following wildcard specifiers are permitted in the searchPattern parameter.

Wildcard character

Description

*

Zero or more characters.

?

Exactly one character.

The order of the returned file names is not guaranteed; use the Sort() method if a specific sort order is required.

Wildcards are permitted. For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". The searchPattern string "s*" searches for all file names beginning with the letter "s". If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array.

NoteNote:

When using the asterisk wildcard character in a searchPattern (for example, "*.txt"), the matching behavior varies depending on the length of the specified file extension. A searchPattern with a file extension of exactly three characters returns files with an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern. A searchPattern with a file extension of one, two, or more than three characters returns only files with extensions of exactly that length that match the file extension specified in the searchPattern. When using the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files in a directory, "file1.txt" and "file1.txtother", a search pattern of "file?.txt" returns only the first file, while a search pattern of "file*.txt" returns both files.

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.

NoteNote:

Because this method checks against file names with both the 8.3 file name format and the long file name format, a search pattern similar to "*1*.txt" may return unexpected file names. For example, using a search pattern of "*1*.txt" will return "longfilename.txt" because the equivalent 8.3 file name format would be "longf~1.txt".

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

The following example counts the directories and files that contain the specified letter.

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        try 
        {
            DirectoryInfo di = new DirectoryInfo(@"c:\");

            // Get only subdirectories that contain the letter "p."
            DirectoryInfo[] dirs = di.GetDirectories("*p*");

            Console.WriteLine("Number of directories with a p: {0}", dirs.Length);

            // Count all the files in each subdirectory that contain the letter "e." 
            foreach (DirectoryInfo diNext in dirs) 
            {
                Console.WriteLine("The number of files in {0} with an e is {1}", diNext, 
                    diNext.GetFiles("*e*").Length);
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Show: