Hello all
Thanks all! This was my first post and so many people are helping!
Thank you very much!
I've once made a class that replaces certain characters (code below),
but as most of you suggested it also needs a temp file to be created
and then copies the file to the original location.
I just got the idea using XML instead of bare text files since
manipulations seem to be much easier using the SelectSingleNode,
InnerXml and ReplaceChild method of XmlElement. I found a pretty good
article here (might help someone else too)
http://www.codeproject.com/soap/myXPath.asp
Code to replace strings in a textfile here (I used this for a
commandline search and replace tool downloadable at
http://www.tomtown.net/?com=tech&sco...browse&cid=002 ):
// =================================================================
public static void ReplaceString(string textFileName, string
searchStr, string replaceStr, int backup)
{
string tempFileName = Path.GetTempFileName();
StreamReader sr = null;
sr = new StreamReader(textFileName,
Encoding.GetEncoding("windows-1252"));
StreamWriter sw = null;
sw = new StreamWriter(tempFileName, false,
Encoding.GetEncoding("windows-1252"));
string line;
System.Text.StringBuilder newline = new
System.Text.StringBuilder();
while ((line = sr.ReadLine()) != null)
{
string correctString = line.Replace(searchStr,
replaceStr);
sw.WriteLine(correctString);
}
sr.Close();
sw.Close();
if (backup == 1)
{
if (File.Exists(textFileName + "_bak"))
File.Delete(textFileName + "_bak");
File.Move(textFileName, textFileName + "_bak");
}
File.Delete(textFileName);
File.Move(tempFileName, textFileName);
}
// =================================================================
Thanx again for all your efforts!!!
Tom