Friday, July 26, 2013

Find and Replace Text in Text / HTML Files

public void ReplaceInFile(string filePath,string replaceText,string findText)
{
    try
    {
         System.IO.StreamReader objReader;
         objReader= new System.IO.StreamReader(filePath);
         string content= objReader.ReadToEnd();
         objReader.Close();
         content= Regex.Replace(content,findText,replaceText);
         StreamWriter writer= new StreamWriter(filePath);
         writer.Write(content);
         writer.Close();
    }
     catch(IOException iexp)
{
   throw;
}