[英]Directory.GetFiles searchPattern inconsistent with documentation
I've seen this question: Is this a bug in DirectoryInfo.GetDirectories(string searchPattern)?我见过这个问题: 这是 DirectoryInfo.GetDirectories(string searchPattern) 中的错误吗?
It is not entirely consistent with my problem, so I'll ask anyway.这与我的问题并不完全一致,所以我还是会问。
My code is:我的代码是:
var pattern = @"file.*";
foreach (var file in Directory.GetFiles(".", pattern))
{
Console.WriteLine(file);
}
In my current directory I have file
and file.txt
.在我当前的目录中,我有file
和file.txt
。 As (un)expected, both files are found.正如(未)预期的那样,两个文件都找到了。
Is it a bug or is it documented somewhere ?它是一个错误还是记录在某处? I've found this issue on Mono and wanted to file a bug there, but I've checked on VS2012 first and it seems to occur as well...我在 Mono 上发现了这个问题,并想在那里提交一个错误,但我首先检查了 VS2012,它似乎也发生了......
Of course I know how to filter the result, but it... angers me anyway, so I'd like to know the sources.我当然知道如何过滤结果,但它……无论如何都让我很生气,所以我想知道来源。
This is how Windows (and DOS) interpret wildcards.这就是 Windows(和 DOS)解释通配符的方式。 Unlike UNIX, Windows at one time actually distinguished, internally, between the "name" portion of a filename and the "extension" portion (this is where the notorious "8.3" limit comes from).与 UNIX 不同,Windows 曾经在内部实际上区分了文件名的“名称”部分和“扩展名”部分(这是臭名昭著的“8.3”限制的来源)。 It doesn't really make this internal distinction anymore, but the legacy of this lives on in Windows.它不再真正区分这种内部区别,但这种遗产仍然存在于 Windows 中。
So Windows/DOS search patterns search for two components, the name, and the extension.因此,Windows/DOS 搜索模式搜索两个组件,名称和扩展名。 The pattern file.*
matches any file named file
with an extension of *
(ie, any extension at all).模式file.*
匹配任何名为file
且扩展名为*
file
(即,任何扩展名)。 Both file
and file.txt
meet this criteria. file
和file.txt
满足此条件。
I would tend to agree that this doesn't make a lot of sense, but that's how it is.我倾向于同意这没有多大意义,但事实就是如此。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.