I am currently making a text file of three different types of accounts, which could be FreeTest, BasicTest, or PremiumTest. I am looking for a solution that will edit a line where the account number is located and edit the properties in that line. Here is what I have tried on the else block. I need to update the properties on the text file line with the parameter from account
that is being passed in to this function.
public void SaveAccount(Account account)
{
if (!fileAccounts.Any(x => x.AccountNumber == account.AccountNumber))
{
fileAccounts.Add(account);
string path = @"C:\testfolder\accounts.txt";
string[] line = new string[fileAccounts.Count + 1];
//line[0] = "AccountNumber,Name,Balance,Type";
int i = 0;
foreach (var a in fileAccounts)
{
line[i] = a.AccountNumber + "," + a.Name + "," + a.Balance + "," + a.Type.ToString().Substring(0, 1);
i++;
}
File.AppendAllLines(path, line);
} else
{
string path = @"C:\testfolder\accounts.txt";
//var line = fileAccounts.Where(x => x.AccountNumber == account.AccountNumber);
using (var reader = new StreamReader(path))
{
var index = 0;
while(!reader.EndOfStream)
{
var line = reader.ReadLine().Split(',');
var _accountNumber = line[0];
var _name = line[1];
var _balance = line[2];
var _type = line[3].ToString().Substring(0, 1);
}
}
//File.WriteAllLines(path, line);
}
}
accounts.txt
AccountNumber,Name,Balance,Type
10001,Free Account,100,F
20001,Basic Account,500,B
30001,Premium Account,1000,P