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 = 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];
}
}
}