I need to make changes to a web.config, so I need to read up until the point where I need to make the change, make the change and then write my update out to the file.
So let's say the file contained:
<add key="Version_PSM" value="2.2.0"/>
<add key="Version_MLF" value="2.0.3"/>
And I needed to update the version pf Version_PSM to "2.1". What's the best way to do this? I've tried opening a FileStream and then creating a StreamReader and a StreamWriter using it, but that doesn't work. As I read lines from the file looking for the key I want to update the Writer stays in position at the beginning of the file, so when I write it doesn't overwrite what I've just read - it writes it to the top of the file. So first I tried something like this:
// Repeat in a loop until I find what I'm looking for...
string readLine = sr.ReadLine();
sw.WriteLine(readline);
which advances the position of the writer, but duplicates what's in the file. I need to position the writer to overwrite the text that I want to update and leave everything else as-is.
So I tried just:
readLine = sr.ReadLine();
sw.WriteLine();
but that just writes blanks to the file.
There's gotta be an easy answer here that I'm just missing!