C#在网页上设置字符集

C#在网页上设置字符集,c#,parsing,character-encoding,C#,Parsing,Character Encoding,我需要在GUI应用程序中设置字符集 应用程序获取网页解析结果并在文本框中显示结果 网页字符集-Windows-1251 我的显示器显示黑色菱形 谢谢 在windows窗体上,使用丰富的文本框。没有足够的信息(AFAICT)来确定“真正的问题”是什么,但如果有帮助,区域性信息存储在每个线程的基础上,您可以通过System.Threading.thread.CurrentThread.CurrentUICulture.TextInfo查找当前线程使用的代码页-请参阅。如果需要,可以将CurrentU

我需要在GUI应用程序中设置字符集

应用程序获取网页解析结果并在文本框中显示结果

网页字符集-Windows-1251

我的显示器显示黑色菱形

谢谢


在windows窗体上,使用丰富的文本框。

没有足够的信息(AFAICT)来确定“真正的问题”是什么,但如果有帮助,区域性信息存储在每个线程的基础上,您可以通过System.Threading.thread.CurrentThread.CurrentUICulture.TextInfo查找当前线程使用的代码页-请参阅。如果需要,可以将CurrentUICulture设置为其他值


不过,了解无法显示的确切字符会更有帮助。:)根据我的经验,只要安装了正确的字体,所有utf8字符串都显示得很好(尽管我不记得这是否意味着要在Windows中安装语言包)

Hmm。。。你需要详细说明你的具体问题。就我个人而言,我无法理解您要的是什么。仍然不清楚您要的是什么。我在这里看到的是:您正在从windows 1251编码的Url读取数据(html?)。然后对数据应用正则表达式。然后将匹配项连接到控件的文本属性(textbox?)。你说你需要“在GUI应用程序中设置字符集”,因为你得到的是黑色菱形。这仍然不足以让我们解决您的问题。UTF8是.NET中的默认字符集。您可能需要将windows-1251转换为UTF-8。我不知道,因为没有足够的信息进行下去。与字符集问题无关,但对于下载内容,您可以通过使用WebClient.DownloadingString使事情变得更简单。此外,获取正则表达式组值的“更好”方法是使用group[1]。value(尽管您可以看出,ToString会返回该值)
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Net;

namespace WindowsFormsApplication1{
public partial class Form1 : Form{
    public Form1(){
        InitializeComponent();
    }
    private void get_field_Click(object sender, EventArgs e){
        string url = url_field.Text;
        string pattern = pattern_field.Text;
        string html = string.Empty;

        HttpWebRequest  myHttpWebRequest  = (HttpWebRequest)HttpWebRequest.Create(url);
        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
        StreamReader result = new StreamReader(myHttpWebResponse.GetResponseStream(), Encoding.GetEncoding(1251));
        html = result.ReadToEnd();

        MatchCollection matches = Regex.Matches(html, pattern);

        foreach(Match title in matches){
            GroupCollection group = title.Groups;
            result_field.Text += group[1].ToString() + "\n\n\n";
        }
    }
}