Note on .NET Directory.GetFiles() method and three-letter file extensions in the search pattern

Posted: (EET/GMT+2)

 

If you are processing files in your .NET and C# applications, chances are you have used the Directory.GetFiles() method to enumerate files inside a folder (directory). This works easily with the GetFiles method, but there's one special case that might get you by surprise. This is related to exactly three-letter file extensions, like ".xls" or ".doc".

This is what the MSDN documentation says about this particular situation:

When you use the asterisk wildcard character in a searchPattern such as "*.txt", the number of characters in the specified extension affects the search as follows:

If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. For example, "*.xls" returns both "book.xls" and "book.xlsx".

In all other cases, the method returns files that exactly match the specified extension. For example, "*.ai" returns "file.ai" but not "file.aif".

Sometimes, you might get extra files in your file listing, if you are using three-letter file extensions. Something to keep in mind!