Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System.IO Namespace
Directory Class
Directory Methods
GetFiles Method
 GetFiles Method (String, String, Se...

  Switch on low bandwidth view
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
Directory..::.GetFiles Method (String, String, SearchOption)

Returns the names of files in the specified directory that match the specified search pattern, using a value to determine whether to search subdirectories.

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

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

Parameters

path
Type: System..::.String
The directory to search.
searchPattern
Type: System..::.String
The search string to match against the names of files in path. The parameter cannot end in two periods ("..") or contain two periods ("..") followed by DirectorySeparatorChar or AltDirectorySeparatorChar, nor can it contain any of the characters in InvalidPathChars.
searchOption
Type: System.IO..::.SearchOption
One of the SearchOption values that specifies whether the search operation should include all subdirectories or only the current directory.

Return Value

Type: array<System..::.String>[]()[]
A String array containing the names of files in the specified directory that match the specified search pattern. File names include the full path.
ExceptionCondition
IOException

path is a file name.

UnauthorizedAccessException

The caller does not have the required permission.

ArgumentException

path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.

-or-

searchPattern does not contain a valid pattern.

ArgumentNullException

path or searchpattern is nullNothingnullptra null reference (Nothing in Visual Basic).

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.

DirectoryNotFoundException

The specified path is invalid (for example, it is on an unmapped drive).

The file names include the full path.

If there are no files, or no files match the searchPattern parameter, this method returns an empty array.

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

The following wildcard specifiers are permitted in the searchPattern parameter.

Wildcard character

Description

*

Zero or more characters.

?

Exactly zero or one character.

Characters other than the wildcard specifiers represent themselves. For example, the searchPattern string "*t" searches for all names in the path parameter ending with the letter "t". The searchPattern string "s*" searches for all names in path beginning with the letter "s".

NoteNote:

When using the asterisk wildcard character in a searchPattern, such as "*.txt", the matching behavior when the extension is exactly three characters long is different than when the extension is more or less than three characters long. A searchPattern with a file extension of exactly three characters returns files having 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 having 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, "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.

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" returns "longfilename.txt" because the equivalent 8.3 file format is "LONGFI~1.TXT".

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.

The path parameter is not case-sensitive.

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

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

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
Missing parameter makes the method invalid      somos   |   Edit   |   Show History
The problem is that the method can throw an UnauthorizedAccessException when looking at a folder that is n folders below the starting folder and then the search will stop and you won't get any return. With a simple parameter like [ bool ignoreExceptions ] that allows you to do deep searches thru the drive the method will be usable.

As you don't know at compile time when you will get an UnauthorizedAccessException you can't use the Directory.GetFiles() never because sometimes it won't work.
Sample Using PowerShell      Thomas Lee   |   Edit   |   Show History
 
# get-files.ps1
# Gets files using system.io.directory.getfiles()
# Thomas Lee - tfl@psp.co.uk
 
# Get all files in C:\Foo\test
"Files in C:\Foo\test"
[system.IO.Directory]::GetFiles("C:\foo\test")
""
 
# Get all files in c:\foo\test\*.txt
"Files in C:\Foo\test\*.txt"
[system.IO.Directory]::GetFiles("C:\foo\test", "*.txt")
""
 
# Get all files in c:\foo\test\*.* /s
"Files in C:\Foo\test /s"
$s = [system.IO.SearchOption]::AllDirectories
[system.IO.Directory]::GetFiles("C:\foo\test\", "*.*",$s)

This script produces the following output:

PS C:\foo\test> .\get-files.ps1
Files in C:\Foo\test
C:\foo\test\div.xml
C:\foo\test\foo.txt
C:\foo\test\foo1.txt
C:\foo\test\foo1.xls
C:\foo\test\foo1.xlsx
C:\foo\test\foo2.txt
C:\foo\test\shows (Autosaved).xlsx
C:\foo\test\shows.xlsx
C:\foo\test\tfl1.xlsx
C:\foo\test\xlsx1.xlsx
C:\foo\test\xlsx2.xlsx
Files in C:\Foo\test\*.txt
C:\foo\test\foo.txt
C:\foo\test\foo1.txt
C:\foo\test\foo2.txt
Files in C:\Foo\test /s
C:\foo\test\div.xml
C:\foo\test\foo.txt
C:\foo\test\foo1.txt
C:\foo\test\foo1.xls
C:\foo\test\foo1.xlsx
C:\foo\test\foo2.txt
C:\foo\test\shows (Autosaved).xlsx
C:\foo\test\shows.xlsx
C:\foo\test\tfl1.xlsx
C:\foo\test\xlsx1.xlsx
C:\foo\test\xlsx2.xlsx
PS C:\foo\test>
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker