<pre style=”display: inline”>
March 19, 2009
March 9, 2009
RegEx: Replace string within a file
Sample Call
ReplaceInFile(@”x:\setup\P8574.T857402.R45.T107801.CSV”, @”\s{65,}”, System.Environment.NewLine);
/// <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, RegexOptions.Singleline);
StreamWriter writer = new StreamWriter( filePath );
writer.Write( content );
writer.Close();
}