行对行操作中读取c#文本文件的有效方法

行对行操作中读取c#文本文件的有效方法,c#,file-read,C#,File Read,我在读取行到行加法操作的文本文件时遇到问题。我使用了以下语法 StreamReader=newstreamreader(“input.txt”); 弦线; 而((line=reader.ReadLine())!=null) string[]splitted=line.Split('#'); string first=已拆分[0]。Trim(); 字符串秒=已拆分[1]。修剪() 如果文件有以下值,我就用这个语法将输入和文本文件分开 12#15 15#7 13#14 23#31 问题是它只执行最后

我在读取行到行加法操作的文本文件时遇到问题。我使用了以下语法

StreamReader=newstreamreader(“input.txt”);
弦线;
而((line=reader.ReadLine())!=null)
string[]splitted=line.Split('#');
string first=已拆分[0]。Trim();
字符串秒=已拆分[1]。修剪()

如果文件有以下值,我就用这个语法将输入和文本文件分开

12#15

15#7

13#14

23#31

问题是它只执行最后一行。它只计算23和31的和,然后只显示,但我想先加上12和15,然后在文本框中显示,类似地,我想添加其他。请帮助我形成适当的语法。

这个问题很模糊,但是,我建议使用Linq:

您可以根据需要添加任意数量的
选择
(行到行操作)。然后,您可以在
foreach
循环中继续执行项目:

  foreach (var item in source) { 
    ...
    // Let's read some fields from the anonymous object
    var first = item.first;
    var second = item.second; 
    ...
  }
编辑:根据编辑后的问题,您只想总结一下,也可以通过Linq完成:


它不仅读取最后一行,而且在其他迭代中,您永远不会执行任何操作。目前,您只需使用已读取的最新行的值重新分配
,我猜您希望将其保存到列表或类似列表中

StreamReader reader = new StreamReader("input.txt");
string line;
List<string> allLines = new List<string>();
while ((line = reader.ReadLine()) != null)
     allLines.Add(line);
StreamReader=newstreamreader(“input.txt”);
弦线;
List allLines=新列表();
而((line=reader.ReadLine())!=null)
所有行。添加(行);

在这里,您可以测试您的文件并将数据加载到DataTable中,这应该非常简单

DataTable dtTextFileData = new DataTable();
dtTextFileData.Columns.AddRange(new []
{
    new DataColumn("First", typeof(string)), 
    new DataColumn("Second", typeof(string)) 
});

StreamReader file = new StreamReader(@"c:\YourFilePath\input.txt");
string line = file.ReadLine();
while (line != null)
{
    string[] fields = line.Split('#');
    DataRow dr = dtTextFileData.NewRow();
    dr["First"]  = fields[0].ToString();
    dr["Second"] = fields[1].ToString();
    dtTextFileData.Rows.Add(dr);
    line = file.ReadLine();
}

不清楚你在问什么。在
循环中执行什么代码?你说的“它只执行最后一行”是什么意思?看看我的例子@SangamJung,你很快就会发现,在输入
while循环之前,你忘了发出
初始读取()。thanx Toote:有更简洁的方法来编写此代码,这个答案只是解决了您在问题中提到的问题。我已经试着把我的问题弄清楚一点。使用foreach中存储在first和second中的值的语法是什么loop@SangamJung:只是
item.first或/和
第二项第一个
最后一个
,这也可以在Linq的帮助下完成(参见我的编辑)。我已经试着把我的问题弄清楚了一点。还有thanx
  var result = File
    .ReadLines("input.txt")   
    .Select(line => line.Split('#'))
    //.Where(items => items.Length == 2)  // you may want to filter out the lines
    .Sum(items => int.Parse(items[0]) + int.Parse(items[1]));

  txtBox.text = result.ToString();
StreamReader reader = new StreamReader("input.txt");
string line;
List<string> allLines = new List<string>();
while ((line = reader.ReadLine()) != null)
     allLines.Add(line);
DataTable dtTextFileData = new DataTable();
dtTextFileData.Columns.AddRange(new []
{
    new DataColumn("First", typeof(string)), 
    new DataColumn("Second", typeof(string)) 
});

StreamReader file = new StreamReader(@"c:\YourFilePath\input.txt");
string line = file.ReadLine();
while (line != null)
{
    string[] fields = line.Split('#');
    DataRow dr = dtTextFileData.NewRow();
    dr["First"]  = fields[0].ToString();
    dr["Second"] = fields[1].ToString();
    dtTextFileData.Rows.Add(dr);
    line = file.ReadLine();
}