Introduction
When using the DirectoryInfo GetFiles
method, you may notice that sometimes the method returns too many files.
MSDN states that
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
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".
The Code
So to work around these issues, where you think that only *.txt will return files with extensions that match .txt and NOT .txt_Old, here are three extensions:
public static FileInfo[] GetFilesFixed
(this DirectoryInfo directoryinfo, string searchPattern, SearchOption searchOption)
{
if (((searchPattern.Length - (searchPattern.LastIndexOf('.') + 1)) == 3) && !
searchPattern.Substring(searchPattern.LastIndexOf('.')).Contains('*'))
return directoryinfo.GetFiles(searchPattern, searchOption).ToList().
FindAll(F => F.Extension.Length == 4).ToArray();
return directoryinfo.GetFiles(searchPattern, searchOption);
}
public static FileInfo[] GetFilesFixed(this DirectoryInfo directoryinfo, string searchPattern)
{
if (((searchPattern.Length - (searchPattern.LastIndexOf('.') + 1)) == 3) && !
searchPattern.Substring(searchPattern.LastIndexOf('.')).Contains('*'))
return directoryinfo.GetFiles(searchPattern).ToList().FindAll
(F => F.Extension.Length == 4).ToArray();
return directoryinfo.GetFiles(searchPattern);
}
public static FileSystemInfo[] GetFileSystemInfosFixed
(this DirectoryInfo directoryinfo, string searchPattern)
{
if (((searchPattern.Length - (searchPattern.LastIndexOf('.') + 1)) == 3) && !
searchPattern.Substring(searchPattern.LastIndexOf('.')).Contains('*'))
return directoryinfo.GetFileSystemInfos(searchPattern).ToList().FindAll
(F => F.Extension.Length == 4).ToArray();
return directoryinfo.GetFiles(searchPattern);
}
More Information
I have posted the extension on MSDN and on my Blog.
History