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.

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[]

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.

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.

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.

NoteNote

The A search pattern using the asterisk matches all extensions with a wildcard on the end, while a search pattern using the question mark does not use a wildcard. For example, given two files, file1.txt and file1.txtother, in a directory, a search pattern of "file?.txt" returns just 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.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Rename or move a directory.

Directory.Move

DirectoryInfo.MoveTo

Rename or move a file.

File.Move

FileInfo.MoveTo

Delete a directory.

Directory.Delete

DirectoryInfo.Delete

Create a directory.

CreateDirectory

Directory

Create a subdirectory.

CreateSubdirectory

See the files in a directory.

Name

See the subdirectories of a directory.

GetDirectories

GetDirectories

See all the files and all the subdirectories in a directory.

GetFileSystemInfos

Find the size of a directory.

Directory

Determine if a file exists.

Exists

Determine if a directory exists.

Exists

Get file attributes.

GetAttributes

Set file attributes.

SetAttributes

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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

Show:
© 2014 Microsoft