原文地址:
http://www.cnblogs.com/LoveJenny/archive/2012/02/08/2343297.html获取文章的编码格式:
 public static Encoding GetEncoding(string filePath)
public static Encoding GetEncoding(string filePath)

 
         {
{
 if (filePath == null)
            if (filePath == null)

 
             {
{
 throw new ArgumentNullException("filePath");
                throw new ArgumentNullException("filePath");
 }
            }
 Encoding encoding1 = Encoding.Default;
            Encoding encoding1 = Encoding.Default;
 if (File.Exists(filePath))
            if (File.Exists(filePath))

 
             {
{
 try
                try

 
                 {
{
 using (FileStream stream1 = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                    using (FileStream stream1 = new FileStream(filePath, FileMode.Open, FileAccess.Read))

 
                     {
{
 if (stream1.Length > 0)
                        if (stream1.Length > 0)

 
                         {
{
 using (StreamReader reader1 = new StreamReader(stream1, true))
                            using (StreamReader reader1 = new StreamReader(stream1, true))

 
                             {
{
 char[] chArray1 = new char[1];
                                char[] chArray1 = new char[1];
 reader1.Read(chArray1, 0, 1);
                                reader1.Read(chArray1, 0, 1);
 encoding1 = reader1.CurrentEncoding;
                                encoding1 = reader1.CurrentEncoding;
 reader1.BaseStream.Position = 0;
                                reader1.BaseStream.Position = 0;
 if (encoding1 == Encoding.UTF8)
                                if (encoding1 == Encoding.UTF8)

 
                                 {
{
 byte[] buffer1 = encoding1.GetPreamble();
                                    byte[] buffer1 = encoding1.GetPreamble();
 if (stream1.Length >= buffer1.Length)
                                    if (stream1.Length >= buffer1.Length)

 
                                     {
{
 byte[] buffer2 = new byte[buffer1.Length];
                                        byte[] buffer2 = new byte[buffer1.Length];
 stream1.Read(buffer2, 0, buffer2.Length);
                                        stream1.Read(buffer2, 0, buffer2.Length);
 for (int num1 = 0; num1 < buffer2.Length; num1++)
                                        for (int num1 = 0; num1 < buffer2.Length; num1++)

 
                                         {
{
 if (buffer2[num1] != buffer1[num1])
                                            if (buffer2[num1] != buffer1[num1])

 
                                             {
{
 encoding1 = Encoding.Default;
                                                encoding1 = Encoding.Default;
 break;
                                                break;
 }
                                            }
 }
                                        }
 }
                                    }
 else
                                    else

 
                                     {
{
 encoding1 = Encoding.Default;
                                        encoding1 = Encoding.Default;
 }
                                    }
 }
                                }
 }
                            }
 }
                        }
 }
                    }
 }
                }
 catch (Exception exception1)
                catch (Exception exception1)

 
                 {
{
 throw;
                    throw;
 }
                }
 if (encoding1 == null)
                if (encoding1 == null)

 
                 {
{
 encoding1 = Encoding.UTF8;
                    encoding1 = Encoding.UTF8;
 }
                }
 }
            }
 return encoding1;
            return encoding1;
 }
        }

 public static void Main()
public static void Main()


 {
{
 List<string> lstFilePath = new List<string>()
    List<string> lstFilePath = new List<string>()

 
     {
{
 "H:\\TestText\\ansi.txt",
        "H:\\TestText\\ansi.txt",
 "H:\\TestText\\unicode.txt",
        "H:\\TestText\\unicode.txt",
 "H:\\TestText\\utf8.txt"
        "H:\\TestText\\utf8.txt"
 };
    };
 
 
 foreach (string filePath in lstFilePath)
    foreach (string filePath in lstFilePath)

 
     {
{
 using (StreamReader reader = new StreamReader(filePath, GetEncoding(filePath)))
        using (StreamReader reader = new StreamReader(filePath, GetEncoding(filePath)))

 
         {
{
 Console.WriteLine("读取文件" + filePath);
            Console.WriteLine("读取文件" + filePath);
 Console.WriteLine(reader.ReadToEnd());
            Console.WriteLine(reader.ReadToEnd());
 Console.WriteLine("当前编码:" + reader.CurrentEncoding.EncodingName);
            Console.WriteLine("当前编码:" + reader.CurrentEncoding.EncodingName);
 Console.WriteLine("************************************************************");
            Console.WriteLine("************************************************************");
 }
        }
 }
    }
 }
} 
	posted on 2012-05-02 11:22 
漂漂 阅读(968) 
评论(0)  编辑 收藏 引用  所属分类: 
c#开发