Jun 07
Sometimes you need a quick & easy way to search and replace text in a file. The following code shows how it can be done using the static methods on the Regex regular expression class. Because this sample loads the entire file contents in memory, is not appropriate for very large files.
using System; using System.IO; using System.Text.RegularExpressions; /// <summary> /// Replaces text in a file. /// </summary> /// <param name="filePath">Path of the text file.</param> /// <param name="searchText">Text to search for.</param> /// <param name="replaceText">Text to replace the search text.</param> static public void ReplaceInFile( string filePath, string searchText, string replaceText ) { StreamReader reader = new StreamReader( filePath ); string content = reader.ReadToEnd(); reader.Close(); content = Regex.Replace( content, searchText, replaceText ); StreamWriter writer = new StreamWriter( filePath ); writer.Write( content ); writer.Close(); }
Copyright © 2007-8 Tiwebb Ltd. All rights reserved. This material may not be published, broadcast, rewritten or redistributed without explicit permission from Tiwebb Ltd.

Amazing..i went through loads of method for replace but this is the most sleek and crisp way..thanks for posting
I ran your code on a 170 MB file and it failed miserably. Could you suggest better ways of modifying this program so that it works on huge datasets as well ?
Rakesh: Yes, of course this code fails miserably on a 170MB file. That's why the article states, "Because this sample loads the entire file contents in memory, it is not appropriate for very large files."
To search/replace in large files, you need to load and save the files in manageable-size blocks. Here is code on how to read/write files using TextReader/TextWriter:
http://www.csharphelp.com/archives/archive24.html
Timm your link doesnt say jack about replacing though and thats what this is for.
That's correct, the other article just shows how to read/write a large file. You can use the Regex method shown in my code above to actually do the replace.
But there's a hitch… what about a search term that spans two consecutive blocks of data that are read separately? Maybe that can be a topic for a subsequent article.
Good article. I noticed the comments about "very large" files. What constitutes "very large"?
I am working on an application that will be fine today because the files it reads are fairly small. I want to check the file size and determine how I should perform the replace based on the size. What would you suggest as an appropriate size?
Thanks in advance for your response.
Thanks! It really helped me!
I must comment on Rakesh's bad manners!
The article states the methods limitations! What part of "is not appropriate for very large files." can he not understand!
Then he compounds his bad manners by moaning about a link the author gives him.
If I was the author, I would not have replied to such an ill mannered person.
Timm, I thank you for your time and effort.
You will need…
using System.Text.RegularExpressions; // for Regex