DirectoryInfo.GetFiles Method (String)
Updated: May 2010
Returns a file list from the current directory matching the given search pattern.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- searchPattern
- Type: System.String
The search string, such as "*.txt".
| Exception | Condition |
|---|---|
| ArgumentException |
searchPattern contains one or more invalid characters defined by the GetInvalidPathChars method. |
| ArgumentNullException |
searchPattern is null. |
| DirectoryNotFoundException |
The path is invalid (for example, it is on an unmapped drive). |
| SecurityException |
The caller does not have the required permission. |
If there are no files in the DirectoryInfo, this method returns an empty array.
The following wildcard specifiers are permitted in the searchPattern parameter.
|
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.
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. |
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.
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 FileInfo properties:
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()); } } }
-
FileIOPermission
for access to the path and for reading from files and directories. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.PathDiscovery
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note