System.IO.DirectoryInfo.GetFiles Method

Returns a file list from the current directory matching the given search pattern.

Syntax

public FileInfo[] GetFiles (string searchPattern)

Parameters

searchPattern
The search string to match against the names of files. This parameter can contain a combination of valid literal path and wildcard (* and ?) characters (see Remarks), but doesn't support regular expressions. The default pattern is "*", which returns all files.

Returns

An array of type System.IO.FileInfo.

Remarks

searchPattern can be a combination of literal and wildcard characters, but doesn't support regular expressions. The following wildcard specifiers are permitted in searchPattern.

* (asterisk)

Zero or more characters in that position.

? (question mark)

Zero or one character in that position.

Characters other than the wildcard are literal characters. For example, the string "*t" searches for all names in ending with the letter "t". The searchPattern string "s*" searches for all names in path beginning with the letter "s".

The DirectoryInfo.EnumerateFiles and DirectoryInfo.GetFiles methods differ as follows:

Therefore, when you are working with many files and directories, DirectoryInfo.EnumerateFiles can be more efficient.

If there are no files in the System.IO.DirectoryInfo, this method returns an empty array.

The following wildcard specifiers are permitted in the searchPattern parameter.

*

Zero or more characters.

?

Exactly one character.

The order of the returned file names is not guaranteed; use the erload:System.Array.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 System.IO.DirectoryInfo, this method returns an empty array.

Note:

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.

Note:

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".

This method pre-populates the values of the following System.IO.FileInfo properties:

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0