Directory.GetFiles returns unexpected results with SearchPattern

前端 未结 2 1662
逝去的感伤
逝去的感伤 2021-01-27 05:25

I\'m working on a batch program that process a big amount of files (more than 50 000 files) and I\'m facing weird behavior with the Directory.GetFiles method.

相关标签:
2条回答
  • 2021-01-27 06:08

    This is the expected behavior of the GetFiles method and it's same on Windows as well, if you search in directory with .pdf it will pick files with extensions .pdfa or *.pdfaaa, you would need to put a Where() yourself like:

    Directory.GetFiles(directory, "*.pdf").Where(item => item.EndsWith(".pdf"));
    

    As you can see that when we search in windows It is giving the same result as your code was giving:

    For the reason of why the GetFiles is behaving that way please have a look here and you might also want to look at this post as well

    0 讨论(0)
  • 2021-01-27 06:09

    As explained by @luaan and by @hans-passant (thanks a lot !) I do not found the file with the .pdfa extension, because the 8.3 format is disabled on my hard drive.

    On a hard drive with the 8.3 format enabled, the method behaves like stated in the doc.

    The GetFiles has a different behavior with the setting enabled or not.

    0 讨论(0)
提交回复
热议问题