2012-01-14 36 views
0

我需要在GUI应用程序中设置字符集。C#在网页上设置字符集

应用程序获取解析网页的结果并在TextBox中显示结果;

网页字符集 - Windows-1251。

我的显示器显示黑色菱形。

Thx!

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"; 
     } 
    } 
} 
+0

嗯...您需要详细说明你的特定问题。我无法理解你要求的是什么。 – 2012-01-14 03:31:26

+0

好的。我是编辑帖子,添加代码。 – systemiv 2012-01-14 03:35:04

+0

仍然不清楚你要求的是什么。 – 2012-01-14 03:36:24

回答

0

在你的Windows窗体上,使用富文本框。

0

有没有足够的信息(AFAICT)来确定什么是'真正的问题',但万一它是有用的,文化信息存储在每个线程的基础上,你可以找到代码页使用的当前线程与System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo - 请参阅http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo.textinfo.aspx。如果需要,您可以将CurrentUICulture设置为其他内容。

尽管知道没有显示的确切字符会更有帮助。 :)根据我的经验,只要我安装了正确的字体(尽管我不记得是否意味着将语言包安装到Windows中),所有utf8字符串都显示正常。