April 18, 2012

DirectoryInfo GetFiles Method returns to many files

Working with the DirectoryInfo.GetFiles method in the .Net Framework, I notised that the method, returned to many files in some setups.
Looking at the MSDN I found the following statement:

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.

and

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

Microsoft is saying that the following searchpattern is in effect:

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.


So I came up with these three extensions for the DirectoryInfo to work around the issue.

            /// <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 here: 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);
            }

            /// <summary>
            /// Returns a file list from the current directory matching the given searchPattern.
            /// This is as fix for 8.3 filename standard. more information here: 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);
            }

            /// <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 here: 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);
            }

No comments:

Post a Comment

Total Pageviews