我如何在文件内容中编码字符串

本文关键字:编码 编码字符 字符串 文件 | 更新日期: 2023-09-27 18:10:37

这是我之前在form1构造器中所做的:

client.Encoding = System.Text.Encoding.GetEncoding(1255);
page = client.DownloadString("http://rotter.net/scoopscache.html");

client = WebClient变量Page = string变量

但是我改变了它,我在form1构造器中做:

page = OffLineDownload.offlineHtmlFile1();

现在的页面有相同的内容,但没有下载它。我怎么能得到编码到1255现在,因为一些内容的页面是在希伯来语?

我试过了:

page = Encoding.GetEncoding(1255).ToString();
page = OffLineDownload.offlineHtmlFile1();

但是它不工作,我后来得到了错误,因为内容不是希伯来语,但一些符号和字符更像gibrish。

离线类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
namespace ScrollLabelTest
{
    class OffLineDownload
    {
        static string offLineHtmlBeforeChanged;
        static string OffLineHtmlAfterChanged;
        public static string offlineHtmlFile1()
        {
            offLineHtmlBeforeChanged = File.ReadAllText(@"C:'Temp'news'news1.htm");
            return offLineHtmlBeforeChanged;
        }
        public static string offlineHtmlFile2()
        {
            OffLineHtmlAfterChanged = File.ReadAllText(@"C:'Temp'news'news2.htm");
            return OffLineHtmlAfterChanged;
        }
    }
}

我如何在文件内容中编码字符串

From System.Net.WebClient.DownloadString

byte[] bytes = this.DownloadDataInternal(address, out request);
string @string = this.GuessDownloadEncoding(request).GetString(bytes);

相当于

byte[] bytes = File.ReadAllBytes(filename);
string @string = Encoding.GetEncoding(1255).GetString(bytes);

尽管正如Hans指出的那样,最好一次完成

string @string = File.ReadAllText(filename, Encoding.GetEncoding(1255));