And what is wrong with using
string.Replace()
?
if (original==null) return string.Empty
else return original.Replace(oldValue, newValue);
Also, you are not reading file line by line, you read it whole at once into one string. If you have "This is actual sample" in 2 or more places, only first will be replaced.
Reading and replaceing line by line:
private void Transform_Click(object sender, EventArgs e)
{
FileStream fread = new FileStream(despath, FileMode.Open);
StreamReader sfread = new StreamReader(fread);
FileStream filewrite = new FileStream(despath, FileMode.Create);
StreamWriter filew = new StreamWriter(filewrite);
while (!sfread.EndOfStream)
{
string line = sfread.ReadLine();
string replaceVariable = ReplaceFirstOccurrance(line,
"This is actual Sample",
"This is replaced line"+count.ToString());
filew.Write(replaceVariable);
count++;
}
filew.Close();
filewrite.Close();
fread.Close();
}