我一直在研究這個問題一段時間,我有點卡住了。我有一個文本文件,需要循環讀取所有行,然後將所有子字符串添加到一個最終數字中。問題是,我擁有的是正確讀取並僅爲文件中的第一行生成數字。我不確定是使用'while'還是'for each'。這裏是我的代碼:閱讀文本文件中的所有行並添加它們C#
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()){} – Paparazzi
串[]字= System.IO.File.ReadAllLines(文件路徑); –