C# DirectoryInfo.getFiles 开头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1198721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:39:57  来源:igfitidea点击:

DirectoryInfo.getFiles beginning with

c#directoryinfo

提问by JL.

I've come across some strange behavior trying to get files that start with a certain string.

我在尝试获取以某个字符串开头的文件时遇到了一些奇怪的行为。

Please would someone give a working example on this:

请有人就此给出一个工作示例:

I want to get all files in a directory that begin with a certain string, but also contain the xml extension.

我想获取以某个字符串开头的目录中的所有文件,但还包含 xml 扩展名。

for example:

例如:

 apples_01.xml
 apples_02.xml
 pears_03.xml

I want to be able to get the files that begin with apples.

我希望能够获得以苹果开头的文件。

So far I have this code

到目前为止我有这个代码

 DirectoryInfo taskDirectory = new DirectoryInfo(this.taskDirectoryPath);
 FileInfo[] taskFiles = taskDirectory.GetFiles("*.xml");

采纳答案by Cerebrus

FileInfo[] taskFiles = taskDirectory.GetFiles("apples*.xml");

回答by CodeSpeaker

var taskFiles = taskDirectory.GetFiles("*.xml").Where(p => p.Name.StartsWith("apples"));

回答by Ahmed Atia

GetFiles list files based on Search Pattern you applied.

GetFiles 根据您应用的搜索模式列出文件。

Please refer to DirectoryInfo.GetFilesto know about how to use Search Pattern.

请参阅DirectoryInfo.GetFiles了解如何使用搜索模式。