Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System.IO Namespace
DirectoryInfo Class
GetFiles Method
 GetFiles Method (String, SearchOpti...
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
DirectoryInfo..::.GetFiles Method (String, SearchOption)

Returns a file list from the current directory matching the given searchPattern and using a value to determine whether to search subdirectories.

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Function GetFiles ( _
    searchPattern As String, _
    searchOption As SearchOption _
) As FileInfo()
Visual Basic (Usage)
Dim instance As DirectoryInfo
Dim searchPattern As String
Dim searchOption As SearchOption
Dim returnValue As FileInfo()

returnValue = instance.GetFiles(searchPattern, _
    searchOption)
C#
public FileInfo[] GetFiles(
    string searchPattern,
    SearchOption searchOption
)
Visual C++
public:
array<FileInfo^>^ GetFiles(
    String^ searchPattern, 
    SearchOption searchOption
)
JScript
public function GetFiles(
    searchPattern : String, 
    searchOption : SearchOption
) : FileInfo[]

Parameters

searchPattern
Type: System..::.String
The search string, such as "System*", used to search for all directories beginning with the word "System".
searchOption
Type: System.IO..::.SearchOption
One of the values of the SearchOption enumeration that specifies whether the search operation should include only the current directory or should include all subdirectories.

Return Value

Type: array<System.IO..::.FileInfo>[]()[]
An array of type FileInfo.
ExceptionCondition
ArgumentNullException

searchPattern is nullNothingnullptra 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 searchPattern.

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.

ms143327.alert_note(en-us,VS.90).gifNote:

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.

ms143327.alert_note(en-us,VS.90).gifNote:

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 lists all of the directories and files that begin with the letter "c" in "c:\".

Visual Basic
Imports System
Imports System.IO

Class App
    Public Shared Sub Main()
        ' Specify the directory you want to manipulate.
        Dim path As String = "c:\\"
        Dim searchPattern As String = "c*"

        Dim di As DirectoryInfo = New DirectoryInfo(path)
        Dim directories() As DirectoryInfo = _
            di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly)

        Dim files() As FileInfo = _
            di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly)

        Console.WriteLine( _
            "Directories that begin with the letter 'c' in {0}", path)
        Dim dir As DirectoryInfo
        For Each dir In directories
            Console.WriteLine( _
                "{0,-25} {1,25}", dir.FullName, dir.LastWriteTime)
        Next dir

        Console.WriteLine()
        Console.WriteLine( _
            "Files that begin with the letter 'c' in {0}", path)
        Dim file As FileInfo
        For Each file In files
            Console.WriteLine( _
                "{0,-25} {1,25}", file.Name, file.LastWriteTime)
        Next file
    End Sub
End Class

C#
using System;
using System.IO;

class App
{
    public static void Main()
    {
        // Specify the directory you want to manipulate.
        string path = @"c:\";
        string searchPattern = "c*";

        DirectoryInfo di = new DirectoryInfo(path);
        DirectoryInfo[] directories = 
            di.GetDirectories(searchPattern, SearchOption.TopDirectoryOnly);

        FileInfo[] files = 
            di.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);

        Console.WriteLine(
            "Directories that begin with the letter \"c\" in {0}", path);
        foreach (DirectoryInfo dir in directories)
        {
            Console.WriteLine(
                "{0,-25} {1,25}", dir.FullName, dir.LastWriteTime);
        }

        Console.WriteLine();
        Console.WriteLine(
            "Files that begin with the letter \"c\" in {0}", path);
        foreach (FileInfo file in files)
        {
            Console.WriteLine(
                "{0,-25} {1,25}", file.Name, file.LastWriteTime);
        }
    } // Main()
} // App()

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Documentation Bug      Dave Sexton   |   Edit   |  
The documentation for the searchPattern parameter is invalid. The search pattern, according to the remarks section, applies to file names - not directory names. In practice, the pattern only matches file names.
Tags What's this?: Add a tag
Flag as ContentBug
System.IO.IOException: There is a time and/or date difference between the client and server.      mud209   |   Edit   |  
Any solutions for this? Found on .NET 3.5

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
at System.IO.Directory.GetFiles(String path, String searchPattern, SearchOption searchOption)
at System.IO.Directory.GetFiles(String path)
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker