Вопрос

I made an application to make some changes to aspx files, but after that the problem with special characters were in browsers, I used this method to replace the run aspx files:

   File.WriteAllText(item.pathFile, File.ReadAllText(item.pathFile).Replace(item.OriginalContent, item.ChangedContent));

But after that all aspx pages are appearing with special characters problem in browsers

Это было полезно?

Решение

Encoding enc = GetEncording(item.pathFile);
File.WriteAllText(item.pathFile, 
          File.ReadAllText(item.pathFile, enc).Replace(item.OriginalContent, 
          item.ChangedContent), 
        enc);

by using

public Encoding GetEncording(string filePath)
{
    Encoding enc = Encoding.Default;
    using (var r = new StreamReader(filePath, detectEncodingFromByteOrderMarks: true))
    {
        enc = r.CurrentEncoding;
    }
    return enc;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top