mikebai.com

  • Home
  • dev
  • DotNET
  • M365
  • 搞笑
  • 杂七杂八
  • FocusDict
個人BLOG
it developer
  1. Main page
  2. DotNET
  3. Main content

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

2010-02-26 95hotness 0likes 0comments

  • 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)   
  •             {   
  •                &

  • Tag: Nothing
    Last updated:2010-02-26

    mikebai

    This person is a lazy dog and has left nothing

    Like
    < Last article
    Next article >

    COPYRIGHT © 2025 mikebai.com. ALL RIGHTS RESERVED.

    Theme Kratos Made By Seaton Jiang