
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. The following wildcard specifiers are permitted in searchPattern.
#PDF SEARCH WILDCARD CODE#
' This code produces output similar to the following: This code produces output similar to the following:ĭim di As DirectoryInfo = New DirectoryInfo("C:\ExampleDir")Ĭonsole.WriteLine("No search pattern returns:")Ĭonsole.WriteLine("Search pattern *2* returns:")Ĭonsole.WriteLine("Search pattern test?.txt returns:")Ĭonsole.WriteLine("Search pattern AllDirectories returns:")įor Each fi In di.GetFiles("*", SearchOption.AllDirectories) using System ĭirectoryInfo di = new search pattern returns:") Ĭonsole.WriteLine("Search pattern *2* returns:") Ĭonsole.WriteLine("Search pattern test?.txt returns:") įoreach (var fi in di.GetFiles("test?.txt"))Ĭonsole.WriteLine("Search pattern AllDirectories returns:") įoreach (var fi in di.GetFiles("*", SearchOption.AllDirectories)) The example assumes a directory that has files named log1.txt, log2.txt, test1.txt, test2.txt, test3.txt, and a subdirectory that has a file named SubFile.txt.
#PDF SEARCH WILDCARD HOW TO#
The following example shows how to get a list of files from a directory by using different search options. The caller does not have the required permission. If there are no files, or no files that match the searchPattern string in the DirectoryInfo, this method returns an empty array. The searchPattern string "s*" searches for all file names beginning with the letter "s". For example, the searchPattern string "*.txt" searches for all file names having an extension of "txt". 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. If there are no files in the DirectoryInfo, this method returns an empty array. Therefore, when you are working with many files and directories, EnumerateFiles can be more efficient. When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array. When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

The EnumerateFiles and GetFiles methods differ as follows:

The searchPattern string "s*" searches for all names in path beginning with the letter "s". For example, the string "*t" searches for all names in ending with the letter "t". Zero or more characters in that position.Ĭharacters other than the wildcard are literal characters.

SearchPattern can be a combination of literal and wildcard characters, but it doesn't support regular expressions.
