I have a csv file as shown below
I want to replace the text in the header. Aim is to replace _ underscores by a space.
Sample output
The Code I was trying was by using TextFieldParser
private void CSV_Operation(string path)
{
using (TextFieldParser parser = new TextFieldParser(path))
{
// set the parser variables
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
bool firstLine = true;
while (!parser.EndOfData)
{
string[] fields = parser.ReadFields();
if (firstLine && fields != null)
{
//Code to replace this in actual .csv file, below code does not save these changes in the original file
for (int i = 0; i < fields.Count(); i++)
{
fields[i] = fields[i].Replace('_', ' ');
}
}
// get the column headers
if (firstLine)
{
firstLine = false;
}
}
}
}
I want to persist the changes done to the header text in the original file. Any help?


[asp.net]and[console-application]tags from your question because it's not clear how they relate to your specific question. If they are relevant, please edit your question to saw how.