Samuel <an*******@discussions.microsoft.com> wrote:
For the moment it works like this in my application :
// Open a file for reading
StreamReader streamReader;
streamReader = File.OpenText(fileName);
// Now, read the entire file into a string
string contents = streamReader.ReadToEnd();
streamReader.Close();
// Write the modification into the same file
StreamWriter streamWriter=File.CreateText(fileName);
streamWriter.Write(contents.Replace("String1", "String2"));
streamWriter.Close();
And what don't you like about the way it works at the moment? It could
certainly be more memory efficient if the files are large and your
replacements can work on a line-by-line basis (read a line, do the
replacement, write the line, repeat) - but otherwise, that looks mostly
okay.
I'd suggest using the using (...) construct rather than calling Close
explicitly though, as otherwise your files won't get closed immediately
if you get an exception.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too