我正在使用以下代码:
var di = new DirectoryInfo("path/to/dir");
var matchingFiles = di.GetFiles("*.???);
正如预期的那样,文件如
- 文件.bat
- test.1.xml
- a.txt.txt
被退回。然而,有一些有趣的结果。作为the MS-documentation提到,短名称也被搜索。我还找到了the information about the dot-selector working in .NET 4+ .我仍然看不到这解释了以下结果:
- 文件1.xml.2
- 没有扩展名的文件
Q1:如何解释这个结果。
Q2:如何实现模式*.[3个任意字符]的匹配。
最佳答案
Q2) 您可以使用 LINQ
和 Path
class相反:
var files = Directory.EnumerateFiles("path/to/dir", "*.*")
.Where(file => Path.GetExtension(file).TrimStart('.').Length == 3);
关于c# - Directory.GetFiles(...) 模式 *.???返回有趣的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35647354/