当前位置: 移动技术网 > IT编程>开发语言>c# > C#读取中文文件出现乱码的解决方法

C#读取中文文件出现乱码的解决方法

2019年07月18日  | 移动技术网IT编程  | 我要评论
本文实例讲述了c#读取中文文件出现乱码的解决方法。分享给大家供大家参考。具体分析如下: 先来看这段代码: filestream afile = new file

本文实例讲述了c#读取中文文件出现乱码的解决方法。分享给大家供大家参考。具体分析如下:

先来看这段代码:

filestream afile = new filestream(singlefile,filemode.open);
streamreader sr = new streamreader(afile,encoding.getencoding("gb2312"),true);
string filecontent = sr.readtoend();
afile.close();
processdata pd = new processdata();
pd.procedata(filecontent);

streamreader 使用3个参数 最后一个自动检测utf-8,中文大部分是gb2312,如果不是utf-8,就用gb2312

系统自带utf 检测 ,见如下:

private void detectencoding()
{
 if (this.bytelen >= 2)
 {
 this._detectencoding = false;
 bool flag = false;
 if ((this.bytebuffer[0] == 0xfe) && (this.bytebuffer[1] == 0xff))
 {
  this.encoding = new unicodeencoding(true, true);
  this.compressbuffer(2);
  flag = true;
 }
 else if ((this.bytebuffer[0] == 0xff) && (this.bytebuffer[1] == 0xfe))
 {
  if (((this.bytelen < 4) || (this.bytebuffer[2] != 0)) || (this.bytebuffer[3] != 0))
  {
 this.encoding = new unicodeencoding(false, true);
 this.compressbuffer(2);
 flag = true;
  }
  else
  {
 this.encoding = new utf32encoding(false, true);
 this.compressbuffer(4);
 flag = true;
  }
 }
 else if (((this.bytelen >= 3) && (this.bytebuffer[0] == 0xef)) && ((this.bytebuffer[1] == 0xbb) && (this.bytebuffer[2] == 0xbf)))
 {
  this.encoding = encoding.utf8;
  this.compressbuffer(3);
  flag = true;
 }
 else if ((((this.bytelen >= 4) && (this.bytebuffer[0] == 0)) && ((this.bytebuffer[1] == 0) && (this.bytebuffer[2] == 0xfe))) && (this.bytebuffer[3] == 0xff))
 {
  this.encoding = new utf32encoding(true, true);
  this.compressbuffer(4);
  flag = true;
 }
 else if (this.bytelen == 2)
 {
  this._detectencoding = true;
 }
 if (flag)
 {
  this.decoder = this.encoding.getdecoder();
  this._maxcharsperbuffer = this.encoding.getmaxcharcount(this.bytebuffer.length);
  this.charbuffer = new char[this._maxcharsperbuffer];
 }
 }
}

希望本文所述对大家的c#程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网