Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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#_Regex_Parsing_File Io - Fatal编程技术网

如何打开文件,我有它的部分名称和读取它的行在c#

如何打开文件,我有它的部分名称和读取它的行在c#,c#,regex,parsing,file-io,C#,Regex,Parsing,File Io,我写了以下几行代码来读取c#中的文件并获取其内容,然后将其存储在数组中。问题是,我无法获得文件的全名,即,我有部分文件名,如下所示: string[] lines3 = System.IO.File.ReadAllLines( "C:/Users/Welcome/Desktop/Rounds/Fitness/AUV1/paths/"0.1152370.txt"); int cnt4 = 0; bestPathL3 = new float[lines3.Length, 2];

我写了以下几行代码来读取c#中的文件并获取其内容,然后将其存储在数组中。问题是,我无法获得文件的全名,即,我有部分文件名,如下所示:

string[] lines3 = System.IO.File.ReadAllLines(
    "C:/Users/Welcome/Desktop/Rounds/Fitness/AUV1/paths/"0.1152370.txt");

int cnt4 = 0;    
bestPathL3 = new float[lines3.Length, 2];

foreach (string line in lines3)    
{
    string[] temp = line.Split(' ');
    bestPathL3[cnt4, 0] = (float)double.Parse(temp[0]);
    bestPathL3[cnt4, 1] = (float)double.Parse(temp[1]);
    cnt4++;
}

但是,例如,文件名是0.115237052475505

您应该迭代其名称包含您的
partialName
的文件。因此,尝试使用
目录.EnumerateFiles
并使用linq
where
子句操作应用搜索模式

var files = Directory.EnumerateFiles("C:\\path", "*.*", SearchOption.AllDirectories)
    .Where(s => s.Contains("partialFileName"));
int cnt4 = 0;
foreach (var file in files)
{
    var lines3 = System.IO.File.ReadAllLines(file);
    bestPathL3 = new float[lines3.Length, 2];
    foreach (string line in lines3)
    {
        string[] temp = line.Split(' ');
        bestPathL3[cnt4, 0] = (float)double.Parse(temp[0]);
        bestPathL3[cnt4, 1] = (float)double.Parse(temp[1]);
        cnt4++;
        //Do something with bestPathL3
    }
}

那么,您想在不知道文件名的情况下修改文件吗?如果您只有部分文件名,那么如何知道您正在读取正确的文件?您可以有两个文件,文件名中的数字相同,但“0.1152370”后面的数字不同听起来您需要先获取文件名。尝试采用搜索模式的重载