c# 如何在从一个文件中搜索后从多个文件中读取同一行?

c# how to read same line from multiple files after a search from 1 of the files?

这与我正在做的事情类似,我必须同时按顺序处理 3 个文件,当找到字符串时,它会显示每个文件该行的数据。到目前为止,我的问题是它没有从第二个和第三个文件中正确读取数据,它只读取第一行。我的第二个问题,我正在尝试使用

String.Compare(string1, searchBox, true) 

但我不太确定将它放在哪里,因此搜索过程中会忽略空格 (trim) 或大写字母。代码:

    string string1, string2, string3, searchBox;

    StreamReader file1, file2, file3;

    file1 = File.OpenText("data1.dat");
    file2 = File.OpenText("data2.dat");
    file3 = File.OpenText("data3.dat");

    string1 = file1.ReadLine();
    string2 = file2.ReadLine();
    string3 = file3.ReadLine();

    searchBox = searchTxtBox.Text.Trim();

    while ((string1 = file1.ReadLine()) != null)
    {
          if (string1.Contains(searchBox))
          {
           infoListBox.Items.Add(string1 + "====" + string2 + "====" + string3);
           break;
          }
    }

file1.Close();
file2.Close();
file3.Close();
string string1, string2, string3, searchBox;

StreamReader file1, file2, file3;

file1 = File.OpenText("data1.dat");
file2 = File.OpenText("data2.dat");
file3 = File.OpenText("data3.dat");

searchBox = searchTxtBox.Text.Trim();

while ((string1 = file1.ReadLine()) != null
    && (string2 = file2.ReadLine()) != null 
    && (string3 = file3.ReadLine()) != null)
{
      if (string1.IndexOf(searchBox, StringComparison.CurrentCultureIgnoreCase) >= 0)
      {
       infoListBox.Items.Add(string1 + "====" + string2 + "====" + string3);
       break;
      }
}

file1.Close();
file2.Close();
file3.Close();