After following this topic I am able to create
the new row but my question is how do
I save or write the new line to the file?
I tried ‘StreamWriter’ but it only writes the newly
created line.
Any suggestions please?
Here is my code so far:
string path = @"C:/CSV.txt";
string[] lines = File.ReadAllLines(path);
var splitlines = lines.Select(l => l.Split(','));
foreach (var line in splitlines)
{
if(line[1].Contains("34"))
{
line[1] = "100";
var newline = string.Join(",", line);
StreamWriter sr = new StreamWriter(path);
sr.WriteLine(newline);
sr.Close();
}
}
Here is your solution using
StreamReaderclass:If you want to overwrite the file, use this StreamWriter constructor with
append = false.