【发布时间】:2013-08-21 14:04:13
【问题描述】:
我一直在研究这个问题,但我有点卡住了。我有一个文本文件,我需要循环并读取所有行,然后将所有子字符串加在一起以获得一个最终数字。问题是,我所拥有的是正确读取并仅生成文件第一行的数字。我不确定是使用“while”还是“for each”。这是我的代码:
string filePath = ConfigurationSettings.AppSettings["benefitsFile"];
StreamReader reader = null;
FileStream fs = null;
try
{
//Read file and get estimated return.
fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
reader = new StreamReader(fs);
string line = reader.ReadLine();
int soldToDate = Convert.ToInt32(Convert.ToDouble(line.Substring(10, 15)));
int currentReturn = Convert.ToInt32(soldToDate * .225);
//Update the return amount
updateCurrentReturn(currentReturn);
任何建议将不胜感激。
【问题讨论】:
-
而 (reader.ReadLine()) { }
-
string[] words = System.IO.File.ReadAllLines(FilePath);
标签: c# .net for-loop while-loop