Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#
Tip/Trick

DirectoryInfo.GetFiles Returns Too Many Files

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
18 Apr 2012CPOL2 min read 27.4K   5   5
In case where DirectoryInfo.GetFiles returns too many files because of the searchpattern...

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:

C#
/// <summary>
/// Returns a file list from the current directory matching the given 
/// searchPattern and using a value to determine whether to search subdirectories.
/// This is as fix for 8.3 filename standard. more information at: 
/// http://msdn.microsoft.com/en-us/library/ms143327.aspx
/// </summary>
/// <param name="directoryinfo">object to use.</param>
/// <param name="searchPattern">The search string, such as "System*", 
/// used to search for all directories beginning with the word "System".</param>
/// <param name="searchOption">One of the values of the 
/// System.IO.SearchOption enumeration that specifies whether the search 
/// operation should include only the current directory or should 
/// include all subdirectories.</param>
/// <returns>An array of type System.IO.FileInfo.</returns>
/// <exception cref="System.ArgumentNullException"
/// >searchPattern is null.</exception>
/// <exception cref="System.IO.DirectoryNotFoundException">
/// The path is invalid, such as being on an unmapped drive.</exception>
/// <exception cref="System.Security.SecurityException">
/// The caller does not have the required permission.</exception>
/// <remarks>http://msdn.microsoft.com/en-us/library/ms143327.aspx</remarks>
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);
}
C#
/// <summary>
/// Returns a file list from the current directory matching the given searchPattern.
/// This is as fix for 8.3 filename standard. more information at: 
/// http://msdn.microsoft.com/en-us/library/ms143327.aspx
/// </summary>
/// <param name="directoryinfo">object to use.</param>
/// <param name="searchPattern">
/// The search string, such as "*.txt".</param>
/// <returns>An array of type System.IO.FileInfo.</returns>
/// <exception cref="System.ArgumentNullException">
/// searchPattern is null.</exception>
/// <exception cref="System.IO.DirectoryNotFoundException">
/// The path is invalid, such as being on an unmapped drive.</exception>
/// <exception cref="System.Security.SecurityException">
/// The caller does not have the required permission.</exception>
/// <remarks>http://msdn.microsoft.com/en-us/library/ms143327.aspx</remarks>
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);
}
C#
/// <summary>
/// Retrieves an array of strongly typed System.IO.FileSystemInfo objects 
/// representing the files and subdirectories matching the specified search criteria.
/// This is as fix for 8.3 filename standard. more information at: 
/// http://msdn.microsoft.com/en-us/library/ms143327.aspx
/// </summary>
/// <param name="directoryinfo">object to use.</param>
/// <param name="searchPattern">The search string, such as "
/// System*", used to search for all directories beginning with the word "System".</param>
/// <returns>An array of strongly typed FileSystemInfo objects 
/// matching the search criteria.</returns>
/// <exception cref="System.ArgumentNullException">
/// searchPattern is null.</exception>
/// <exception cref="System.IO.DirectoryNotFoundException">
/// The path is invalid, such as being on an unmapped drive.</exception>
/// <exception cref="System.Security.SecurityException">
/// The caller does not have the required permission.</exception>
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

  • 18-04-2012 - Tip posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Denmark Denmark

Comments and Discussions

 
QuestionThank you Pin
Tim Carmichael13-Feb-14 7:20
Tim Carmichael13-Feb-14 7:20 
AnswerRe: Thank you Pin
Paw Jershauge13-Feb-14 20:22
Paw Jershauge13-Feb-14 20:22 
QuestionVery expenisve Pin
rcardare19-Apr-12 15:27
rcardare19-Apr-12 15:27 
AnswerRe: Very expenisve Pin
Paw Jershauge20-Apr-12 1:00
Paw Jershauge20-Apr-12 1:00 
GeneralRe: Very expenisve Pin
rcardare20-Apr-12 3:38
rcardare20-Apr-12 3:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.