Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Directory.GetFiles returns different result on different computer

Tags:

I just noticed a very weird thing in my application. I have a snippet of code that checks if a folder or any sub folder contains .xls files instead of .xlsx. This is because I use EPPlus which can't handle .xls files. On a computer running Windows 10 Home the code below only returns .xls files but not any .xlsx files. I now tried to run the same code on a Windows 10 Pro machine and the code picked up .xlsx files as well. I know I can get only .xls files using Linq but I still would like to know why this can happen.

var filePaths = Directory.GetFiles("C:\\xmlfiles", "*.xls", SearchOption.AllDirectories).ToList();

if (filePaths.Count > 0)
{
    var files = string.Join(",", filePaths);
    throw new Exception($"Folder contains .xls files which EPPlus can't handle. Please convert them first. Files: {files}");
}
like image 816
Ogglas Avatar asked Apr 24 '17 06:04

Ogglas


1 Answers

From MSDN

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

When you use the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, whereas a search pattern of "file*.txt" returns both files.

Above is normal behaviour as MSDN says.

But on your question it may cause from filename convention 8.3 filename. Disable it and look if you get the result as expected.

fsutil behavior set disable8dot3

Also take a look at this question

like image 159
kgzdev Avatar answered Oct 11 '22 20:10

kgzdev