停止 Directory.GetFiles() 中的隐式通配符

发布于 2024-08-06 14:49:34 字数 236 浏览 0 评论 0原文

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

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

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

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

Also returns files like foo.xml_ Is there a way to force it to not do so, or will I have to write code to filter the return results.

This is the same behavior as dir *.xml on the command prompt, but different than searching for *.xml in windows explorer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Smile简单爱 2024-08-13 14:49:34

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

带有文件扩展名的 searchPattern
正好三个字符返回
扩展名为 3 或 3 的文件
更多字符,其中前三个
字符与文件扩展名匹配
在 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();

This behavior is by design. From MSDN (look at the note section and examples given):

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.

You could limit it as follows:

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();
深者入戏 2024-08-13 14:49:34

这是由于windows 8.3的搜索方法造成的。如果您尝试搜索“*.xm”,您将得到 0 个结果。

你可以在.net 2.0中使用它:

string[] fileEntries = 
Array.FindAll<string>(System.IO.Directory.GetFiles(pathName, "*.xml"), 
            new Predicate<string>(delegate(string s)
            {
                return System.IO.Path.GetExtension(s) == ".xml";
            }));

it's due to the 8.3 search method of windows. If you try to search for "*.xm" you'll get 0 results.

you can use this in .net 2.0:

string[] fileEntries = 
Array.FindAll<string>(System.IO.Directory.GetFiles(pathName, "*.xml"), 
            new Predicate<string>(delegate(string s)
            {
                return System.IO.Path.GetExtension(s) == ".xml";
            }));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文