Directory.GetFiles returns unexpected results with SearchPattern

前端 未结 2 1667
逝去的感伤
逝去的感伤 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

提交回复
热议问题