Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.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# 如何在c中使用正则表达式获取文件夹中的文件名#_C#_File Io - Fatal编程技术网

C# 如何在c中使用正则表达式获取文件夹中的文件名#

C# 如何在c中使用正则表达式获取文件夹中的文件名#,c#,file-io,C#,File Io,我需要检索特定文件夹中的所有文件名,但我需要使用正则表达式,在正则表达式中获取文件名取决于文件名中的数字。例如: 适合度-0Chromosom1 我的意思是零在这种情况下,我写了以下内容: //GetFiles on DirectoryInfo returns a FileInfo object. var pdfFiles = new DirectoryInfo("C:/Users/Welcome/Desktop/Rounds/Fitness/AUV" + 1)

我需要检索特定文件夹中的所有文件名,但我需要使用正则表达式,在正则表达式中获取文件名取决于文件名中的数字。例如:

适合度-0Chromosom1

我的意思是零在这种情况下,我写了以下内容:

//GetFiles on DirectoryInfo returns a FileInfo object.

                var pdfFiles = new DirectoryInfo("C:/Users/Welcome/Desktop/Rounds/Fitness/AUV" + 1).GetFiles("fitness-"+geneticIteration+"*"+".txt");

其中
geneticieration
是表示0的数字。是否正确?

是的,上面的代码将返回与目录“C:/Users/Welcome/Desktop/Rounds/fitness/AUV1”中的“fitness-[a number]*.txt”匹配的所有文件名

示例结果可以是(假设“a数”=0):

  • fitness-0Chromosom1.txt
  • 健身-0abc.txt
  • 适合度-0.txt

GetFiles(path)还接受另一个名为“searchPattern”的参数,您可以在其中传递正则表达式。将文件名与正则表达式匹配的另一种方法是使用Regex.IsMatch(路径、模式),它将根据比较/匹配返回true或false。

此处使用的是通配符搜索,而不是正则表达式。
fitness-0Chromosom1
是文件夹或文件的名称吗?您的代码使它看起来像是一个文件名。您的问题很不清楚,但似乎有点linq可以满足您的需要搜索模式是通配符模式而不是正则表达式