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
)
public FileInfo[] GetFiles (
	String searchPattern
)
public function GetFiles (
	searchPattern : String
) : FileInfo[]
Not applicable.

Parameters

searchPattern

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

Return Value

An array of type FileInfo.

Exception typeCondition

ArgumentNullException

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

DirectoryNotFoundException

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

SecurityException

The caller does not have the required permission.

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());
        }
    }
}

import System.*;
import System.IO.*;

class Test
{
    public static void main(String[] args)
    {
        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}", 
                (Int32)dirs.length);
            DirectoryInfo diNext = null;
            // Count all the files in each subdirectory that 
            // contain the letter "e."
            for ( int iCtr=0; iCtr < dirs.length; iCtr++ ) {
                diNext = dirs[iCtr];
                Console.WriteLine("The number of files in {0} with "
                    + "an e is {1}", diNext,
                    (Int32)diNext.GetFiles("*e*").length);
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, 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
Show: