C#自动识别文件编码类
//编码问题目前为止,基本上没人解决,就连windows的IE的自动识别有时还识别错编码呢。--yongfa365//如果文件有BOM则判断,如果没有就用系统默认编码,缺点:没有BOM的非系统编码文件会显示乱码。
//调用方法: EncodingType.GetType(filename)
此工具可以结合“写入文件加上bom头,主要适用于utf8文件”使用using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace GoBuyBatchPublish.publishutils
{
public class EncodingType
//编码问题目前为止,基本上没人解决,就连windows的IE的自动识别有时还识别错编码呢。--yongfa365
//如果文件有BOM则判断,如果没有就用系统默认编码,缺点:没有BOM的非系统编码文件会显示乱码。
//调用方法: EncodingType.GetType(filename)
//来源:http://blog.csdn.net/listlofusage/archive/2007/02/10/1506900.aspx
{
public static System.Text.Encoding GetType(string FILE_NAME)
{
FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
System.Text.Encoding r = GetType(fs);
fs.Close();
return r;
}
public static System.Text.Encoding GetType(FileStream fs)
{
/*byte[] Unicode=new byte[]{0xFF,0xFE};
byte[] UnicodeBIG=new byte[]{0xFE,0xFF};
byte[] UTF8=new byte[]{0xEF,0xBB,0xBF};*/
BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
byte[] ss = r.ReadBytes(3);
r.Close();
//编码类型 Coding=编码类型.ASCII;
if (ss >= 0xEF)
{
if (ss == 0xEF && ss == 0xBB && ss == 0xBF)
{
return System.Text.Encoding.UTF8;
}
else if (ss == 0xFE && ss == 0xFF)
{
return System.Text.Encoding.BigEndianUnicode;
}
else if (ss == 0xFF && ss == 0xFE)
{
return System.Text.Encoding.Unicode;
}
else
{
return System.Text.Encoding.Default;
}
}
else
{
return System.Text.Encoding.Default;
}
}
}
}
好帖还是要收藏,请继续
static/image/common/sigline.gif
30V4A澳规美规开关电源
页:
[1]