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(); }
Related posts:


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
"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."
This is exactly the problem I am faced with. Is there any solution you can suggest?
I have also been dealing with the problem of too large files to completly read into the memory. Now I have created a method that will replace occurences in a text file without dealing with memory issues. How can I post the code to this website so that you can benefit from it? I can't see a place to aquire membership to C# 411. This is also my first comment on this site…
Hi Kevin, you can post the code in a comment here, but the WordPress comment formatting is poor, everything gets left-adjusted.
A better solution, if you are interested, is you can send me the code, and I can publish an article with it, of course giving you full credit. It would be a great service to our readers.
You can contact us at "info at tiwebb dot com"
Thank you!
Hi timm, I have just send an email to the emailaddress you provided.
Hope to be hearing from you.
Here's some good code on this subject:
http://towardsnext.wordpress.com/2009/02/02/replace-data-in-file-c/