背景:
阅读新闻

C#判断文件编码的类(仅常用的几种)

  作者: 今日评论: [字体: ]
  • 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[0] >= 0xEF)   
  •         {   
  •             if (ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF)   
  •             {   
  •                 return System.Text.Encoding.UTF8;   
  •             }   
  •             else if (ss[0] == 0xFE && ss[1] == 0xFF)   
  •             {   
  •                 return System.Text.Encoding.BigEndianUnicode;   
  •             }   
  •             else if (ss[0] == 0xFF && ss[1] == 0xFE)   
  •             {   
  •                 return System.Text.Encoding.Unicode;   
  •             }   
  •             else  
  •             {   
  •                 return System.Text.Encoding.Default;   
  •             }   
  •         }   
  •         else  
  •         {   
  •             return System.Text.Encoding.Default;   
  •         }   
  •     }   
  • }  
  • 另一个例子:

    http://download.csdn.net/source/375539

    来源:
    录入日期:[2010-2-26 16:26:00]
    收藏 推荐 打印 | 录入:mikebai | 阅读:
    文章评论      
    正在加载评论列表...
    评论表单加载中...