我写了以下几行来读取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作为示例...
答案 0 :(得分:1)
您应该迭代其名称包含partialName
的文件。因此,尝试使用Directory.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
}
}