This documentation is archived and is not being maintained.

DirectoryInfo.GetFiles Method (String)

.NET Framework 1.1

Returns a file list from the current directory matching the given searchPattern, such as "*.txt".

[Visual Basic]
Overloads Public Function GetFiles( _
   ByVal searchPattern As String _
) As FileInfo()
[C#]
public FileInfo[] GetFiles(
 string searchPattern
);
[C++]
public: FileInfo* GetFiles(
 String* searchPattern
) [];
[JScript]
public function GetFiles(
   searchPattern : String
) : FileInfo[];

Parameters

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

Return Value

An array of type FileInfo.

Exceptions

Exception Type Condition
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.

Remarks

Wild cards 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.

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.

For an example of using this method, see the Example section below. 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. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Copy a directory. Directory
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 in all subdirectories of 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

Example

[Visual Basic, C#, C++] The following example counts the directories and files that contain the specified letter.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Try
            Dim di As DirectoryInfo = New DirectoryInfo("c:\")
            'Get only subdirectories that contain the letter "p."
            Dim dirs As DirectoryInfo() = di.GetDirectories("*p*")
            Console.WriteLine("Number of directories with a p: {0}", dirs.Length)
            Dim diNext As DirectoryInfo
            ' Count all files in each subdirectory that contain the letter "e."
            For Each diNext In dirs
                Console.WriteLine("The number of files in {0} with an e is {1}", _
                 diNext, diNext.GetFiles("*e*").Length)
            Next

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

[C#] 
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());
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    try {
        DirectoryInfo* di = new DirectoryInfo(S"c:\\");

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

        Console::WriteLine(S"Number of directories with a p: {0}", __box(dirs->Length));

        // Count all the files in each subdirectory that contain the letter S"e."
        Collections::IEnumerator* myEnum = dirs->GetEnumerator();
        while (myEnum->MoveNext()) {
            DirectoryInfo* diNext = __try_cast<DirectoryInfo*>(myEnum->Current);
            Console::WriteLine(S"The number of files in {0} with an e is {1}", diNext, 
                __box(diNext->GetFiles(S"*e*")->Length));
        }
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

.NET Framework Security: 

See Also

DirectoryInfo Class | DirectoryInfo Members | System.IO Namespace | DirectoryInfo.GetFiles Overload List | Working with I/O | Reading Text from a File | Writing Text to a File

Show: