I'm trying to update some part of a string in a file. Currently my code is:
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(filePath);
string[] line = sr.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
List<string> lines = new List<string>();
string eventName = ','+eventNameUpdateTextBox.Text.ToString()+',';
foreach (var l in line)
{
if (l.Contains(eventName))
{
int start = l.IndexOf(eventName);
l.Remove(start, eventName.Length);
l.Insert(start, newNameTextBox.Text.ToString());
lines.Add(l);
}
else
{
lines.Add(l);
}
}
string toCsvOutput = string.Join(Environment.NewLine, lines.ToArray());
But the result I'm getting is the same file as before.
When I try to debug it, I see that function:
l.Insert(start, newNameTextBox.Text.ToString());
Does not change string and returns the same string as at the beginning. Why does this happen? Where am I wrong?