Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DirectoryInfo.getFiles以开头_C#_Directoryinfo - Fatal编程技术网

C# DirectoryInfo.getFiles以开头

C# DirectoryInfo.getFiles以开头,c#,directoryinfo,C#,Directoryinfo,我遇到了一些奇怪的行为,试图获取以某个字符串开头的文件 请有人给出一个工作示例: 我想获取目录中以某个字符串开头,但也包含xml扩展名的所有文件 例如: apples_01.xml apples_02.xml pears_03.xml 我希望能够获得以苹果开头的文件 到目前为止,我有这个代码 DirectoryInfo taskDirectory = new DirectoryInfo(this.taskDirectoryPath); FileInfo[] taskFiles = t

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

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

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

例如:

 apples_01.xml
 apples_02.xml
 pears_03.xml
我希望能够获得以苹果开头的文件

到目前为止,我有这个代码

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

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


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

简单是最好的答案。对于更复杂的场景,您可能在检索所有文件后使用正则表达式GetFiles可能不可靠,因为它同时搜索短名称和长名称。我将把建议改为.GetFiles(),然后使用正则表达式进行过滤。(更好的是,使用.net 4)+1中新的.EnumerateFiles()作为MSDN链接。我正要发布它,但与该站点失去了连接。
var\u FileInfoArray=\u DirectoryInfo.GetFiles(“*.txt”)。其中(x=>x.Name.Contains(\u FileName))
FileInfo[] taskFiles = taskDirectory.GetFiles("apples*.xml");
var taskFiles = taskDirectory.GetFiles("*.xml").Where(p => p.Name.StartsWith("apples"));