Hello!
The problem is this - you need to open the Html-file for reading to find the desired line, replace it with another one and write to the file. I do it while nothing happens

FileStream fi = new FileStream(@"C:\CP1.html", FileMode.OpenOrCreate, FileAccess.ReadWrite); StreamReader rea = new StreamReader(fi); StreamWriter wri = new StreamWriter(fi,false, System.Text.Encoding.GetEncoding(1251)); string time = GetTime(); string t = "<tr><td>" + time +"</td> <td>" + Convert.ToString(Mheight) + "</td> <td>" + Convert.ToString(Mspeed) + "</td> <td>" + Convert.ToString(Mtemp)+ "</td> </tr>"; int i=0; do { i++; string str = rea.ReadLine(); if (str == "<tr class=news-znak>") { str = str.Replace("<tr class=news-znak>", t); wri.WriteLine(str); wri.WriteLine("<tr class=news-znak>"); goto label1; } } while (rea.Peek() != -1); label1: rea.Close(); wri.Close(); 

What could be the error?

  • one
    goto ! among other things, why do you close the file immediately after you find the right line? (btw, you have bad html files, you need <tr class='news-znak'> at least) - zb <tr class='news-znak'>
  • underlines in blue initialization..why? StreamReader rea = new StreamReader (fi); StreamWriter wri = new StreamWriter (fi, false, System.Text.Encoding. GetEncoding (1251)); - Ulyana_Seve

1 answer 1

Reading and writing strings at the same time is not a good idea. I would use an intermediate file:

 string Filter(string s) { if (s == "<tr class=news-znak>") return t; return s; } var srcPath = @"C:\CP1.html"; var tempPath = srcPath + ".temp"; var enc = Encoding.GetEncoding(1251); File.WriteAllLines(tempPath, File.ReadLines(srcPath, enc).Select(Filter) enc); File.Delete(srcPath); File.Move(tempPath, srcPath); 
  • File.WriteAllLines (tempPath, File.ReadAllLines (srcPath, Encoding.GetEncoding (1251)). Select (Filter), Encoding.GetEncoding (1251)); Writes that Select is not available as a method (( - Ulyana_Sever
  • Hmm, strange. (1) What is your version of .NET? (2) Have you forgotten using System.Linq ? - VladD
  • I did a little differently, but the algorithm itself with the intermediate file helped. thank you, everything turned out)) - Ulyana_Seve
  • Regularly everything would be more beautiful :) - ReinRaus
  • @ReinRaus: the Filter function device can be any, albeit with regulars, albeit with manual parsing. :-) - VladD