C# 如何在c中查找和存储文件中的特定行#

C# 如何在c中查找和存储文件中的特定行#,c#,C#,我想搜索一些包含特定数据的行,并将这些数据存储在列表中,我如何才能做到这一点?我已经为我的项目编写了这段代码,但这段代码只能找到并存储包含我想要的数据的第一行,因为所有这些行都以相同的结构开始,例如,我的数据在第100、250、400、660行中重复,所有这些行都用“|PROD | OIL |”表示 double[]oil_prdc=new double[10]; double[]water_prdc=新的double[10]; double[]gas_prdc=新的double[10]; 双[

我想搜索一些包含特定数据的行,并将这些数据存储在列表中,我如何才能做到这一点?我已经为我的项目编写了这段代码,但这段代码只能找到并存储包含我想要的数据的第一行,因为所有这些行都以相同的结构开始,例如,我的数据在第100、250、400、660行中重复,所有这些行都用“|PROD | OIL |”表示

double[]oil_prdc=new double[10];
double[]water_prdc=新的double[10];
double[]gas_prdc=新的double[10];
双[]水注入=新双[10];
双[]气体注入=新双[10];
int length_time=5;
字符串[][]个字符=新字符串[2391][];
字符串[]字符;
字符串[][]计数器=新字符串[20][];
而((line=file.ReadLine())!=null)
{                    
for(int i=0;i字符长度)中断;
}
while(line.Contains(“|WINJ | WAT”))
{
Charss=line.Split(“|”);
for(int jj=0;jj字符长度)中断;
}
while(line.Contains(“|GINJ |被动式”))
{
Charss=line.Split(“|”);
for(intij=0;ij字符长度)中断;
}
_5点温度=新的5点{OIL_PRD=OIL_prdc[i],GAS_PRD=GAS_prdc[i],WATER_PRD=WATER_prdc[i],WATER_INJ=WATER_INJ[i],GAS_INJ=GAS_INJ[i];
总产量[i]。加上(温度);
}
}

如何读取文件的示例:

try 
{
    // Create an instance of StreamReader to read from a file.
    // The using statement also closes the StreamReader.
    using (StreamReader sr = new StreamReader("TestFile.txt")) 
    {
        string line;
        // Read and display lines from the file until the end of 
        // the file is reached.
        while ((line = sr.ReadLine()) != null) 
        {
            Console.WriteLine(line);
        }
    }
}
catch (Exception e) 
{
    // Let the user know what went wrong.
    Console.WriteLine("The file could not be read:");
    Console.WriteLine(e.Message);
}
在每个“行”中搜索特定文本:

如何使用它:

string text = "This is an example string and my data is here";
string data = getBetween(text, "my", "is");

输入文件是文本格式还是其他xml格式?解析文本文件并不简单。做了40年,我做得很好。没有看到实际的文本文件,我无法给出建议。我看到的问题太多了,无法给出答案。像您这样使用While循环通常不会很好地工作。我通常使用一个非常健壮的状态机,它可以消除大量的意大利面代码。如果你想我可以显示我的文本文件,我无法粘贴,因为缺少字符。谢谢你的评论,亲爱的Tobbe×评论只能编辑5分钟×评论只能编辑5分钟×评论只能编辑5分钟
public static string getBetween(string strSource, string strStart, string strEnd)
{
    int Start, End;
    if (strSource.Contains(strStart) && strSource.Contains(strEnd))
    {
        Start = strSource.IndexOf(strStart, 0) + strStart.Length;
        End = strSource.IndexOf(strEnd, Start);
        return strSource.Substring(Start, End - Start);
    }
    else
    {
        return "";
    }
}
string text = "This is an example string and my data is here";
string data = getBetween(text, "my", "is");