gpt4 book ai didi

c# - 在 Directory.GetFiles() 中停止隐式通配符

转载 作者:太空狗 更新时间:2023-10-30 00:59:03 26 4
gpt4 key购买 nike

string[] fileEntries = Directory.GetFiles(pathName, "*.xml");

还返回类似 foo.xml_ 的文件有没有办法强制它不这样做,或者我是否必须编写代码来过滤返回结果。

这与命令提示符下的 dir *.xml 行为相同,但不同于在 Windows 资源管理器中搜索 *.xml

最佳答案

此行为是设计使然。来自 MSDN (查看注释部分和给出的示例):

A searchPattern with a file extension of exactly three characters returns files having an extension of three or more characters, where the first three characters match the file extension specified in the searchPattern.

你可以这样限制它:

C# 2.0:

string[] fileEntries = Array.FindAll(Directory.GetFiles(pathName,  "*.xml"),
delegate(string file) {
return String.Compare(Path.GetExtension(file), ".xml", StringComparison.CurrentCultureIgnoreCase) == 0;
});
// or
string[] fileEntries = Array.FindAll(Directory.GetFiles(pathName, "*.xml"),
delegate(string file) {
return Path.GetExtension(file).Length == 4;
});

C# 3.0:

string[] fileEntries = Directory.GetFiles(pathName, "*.xml").Where(file =>
Path.GetExtension(file).Length == 4).ToArray();
// or
string[] fileEntries = Directory.GetFiles(pathName, "*.xml").Where(file =>
String.Compare(Path.GetExtension(file), ".xml",
StringComparison.CurrentCultureIgnoreCase) == 0).ToArray();

关于c# - 在 Directory.GetFiles() 中停止隐式通配符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1488443/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com