简体   繁体   English

如何过滤具有特定扩展名的 Directory.EnumerateFiles

[英]How to filter Directory.EnumerateFiles with specific extension

I want a list of all xml files in a folder like this:我想要一个文件夹中所有 xml 文件的列表,如下所示:

foreach (var file in Directory.EnumerateFiles(folderPath, "*.xml"))
{
    // add file to a collection
}

However, if I for some reason have any files in folderPath that ends with .xmlXXX where XXX represent any characters, then they will be part of the enumerator.不过,如果我出于某种原因在任何文件folderPath与两端.xmlXXX其中XXX代表任意字符,那么他们将枚举的一部分。

If can solve it easily by doing something like如果可以通过执行类似的操作轻松解决它

foreach (var file in Directory.EnumerateFiles(folderPath, "*.xml").Where(x => x.EndsWith(".xml")))

But it seems a bit odd to me, as I basically have to search for the same thing two times.但这对我来说似乎有点奇怪,因为我基本上必须两次搜索相同的东西。 Is there any way to get the right files directly or am I doing something wrong?有什么方法可以直接获取正确的文件还是我做错了什么?

The is the documented/default behaviour of the wildcard usage with file searching.这是文件搜索中通配符使用的记录/默认行为。

Directory.EnumerateFiles Method (String, String) Directory.EnumerateFiles 方法(字符串,字符串)

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".例如,“*.xls”返回“book.xls”和“book.xlsx”。

Your current approach of filtering twice is the right way.您当前的两次过滤方法是正确的方法。

The only improvement you can do is to ignore case in EndsWith like:您可以做的唯一改进是忽略EndsWith大小写,例如:

x.EndsWith(".xml", StringComparison.CurrentCultureIgnoreCase)

It seems like you cant do it using EnumerateFiles for 3 characters extension, according to MSDN根据MSDN ,您似乎无法使用 EnumerateFiles 进行 3 个字符的扩展

Quote from the article above引自上述文章

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.在 searchPattern 中使用星号通配符(例如“ .txt”)时,指定扩展名中的字符数会影响搜索,如下所示:如果指定的扩展名正好是三个字符长,则该方法返回扩展名以指定的扩展名。 For example, " .xls" returns both "book.xls" and "book.xlsx".例如,“. xls”返回“book.xls”和“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".例如,“. ai”返回“file.ai”而不是“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.例如,给定目录中的两个文件“file1.txt”和“file1.txtother”,搜索模式“file?.txt”仅返回第一个文件,而搜索模式“file .txt”返回两个文件。

Therefore using the .Where extension seems like the best solution to your problem因此,使用 .Where 扩展名似乎是您问题的最佳解决方案

Yes, and this design is stupid, stupid, stupid!是的,这个设计是愚蠢的,愚蠢的,愚蠢的! It shouldn't do that.它不应该那样做。 And it's annoying too!而且也很烦人!

That said, it appears this is what is happening: It actually searches both the long and short filenames.也就是说,看起来这就是正在发生的事情:它实际上搜索长文件名和短文件名。 So files with longer extensions will have a short filename with the extension truncated to three characters.因此,具有较长扩展名的文件将具有较短的文件名,并将扩展名截断为三个字符。

And on newer versions of Windows, the short filenames may be disabled.在较新版本的 Windows 上,可能会禁用短文件名。 So the behavior on newer systems will actually be what you would expect, and what it should've been in the first place.因此,较新系统上的行为实际上将是您所期望的,并且首先应该是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM