I am attempting to replace a value in a .csv file, with "existing value" + "a".In other words, I would like to append an "a" to all rows, one specific column, but omit the header row. My code below is successful in the bottom row of the file, but I believe the placement of my foreach loop relative to the while loop is causing an issue. Is there something glaring that I am missing? If so, I apologize as I am a beginner. Please note, I know there is some redundant code in there that I am cleaning up once working. Thanks.
public static void ReadReplace()
{
using (StreamReader sr = new StreamReader(@"C:\TestDirectory\Test.csv"))
{
foreach(string test in File.ReadLines(@"C:\TestDirectory\Test.csv"))
{
String line;
while ((line = sr.ReadLine()) != null)
{
string[] parts = line.Split(',');
string serviceId = parts[34].Trim('"');
string appendedServiceId = serviceId + "a";
string text = File.ReadAllText(@"C:\TestDirectory\Test.csv");
text = text.Replace(serviceId, appendedServiceId);
File.WriteAllText(@"C:\TestDirectory\TestUpdated.csv", text);
}
}
}
}