随笔 - 224  文章 - 41  trackbacks - 0
<2024年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

享受编程

常用链接

留言簿(11)

随笔分类(159)

随笔档案(224)

文章分类(2)

文章档案(4)

经典c++博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜

原文地址:http://www.cnblogs.com/LoveJenny/archive/2012/02/08/2343297.html




获取文章的编码格式:

public static Encoding GetEncoding(string filePath)
        
{
            
if (filePath == null)
            
{
                
throw new ArgumentNullException("filePath");
            }

            Encoding encoding1 
= Encoding.Default;
            
if (File.Exists(filePath))
            
{
                
try
                
{
                    
using (FileStream stream1 = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                    
{
                        
if (stream1.Length > 0)
                        
{
                            
using (StreamReader reader1 = new StreamReader(stream1, true))
                            
{
                                
char[] chArray1 = new char[1];
                                reader1.Read(chArray1, 
01);
                                encoding1 
= reader1.CurrentEncoding;
                                reader1.BaseStream.Position 
= 0;
                                
if (encoding1 == Encoding.UTF8)
                                
{
                                    
byte[] buffer1 = encoding1.GetPreamble();
                                    
if (stream1.Length >= buffer1.Length)
                                    
{
                                        
byte[] buffer2 = new byte[buffer1.Length];
                                        stream1.Read(buffer2, 
0, buffer2.Length);
                                        
for (int num1 = 0; num1 < buffer2.Length; num1++)
                                        
{
                                            
if (buffer2[num1] != buffer1[num1])
                                            
{
                                                encoding1 
= Encoding.Default;
                                                
break;
                                            }

                                        }

                                    }

                                    
else
                                    
{
                                        encoding1 
= Encoding.Default;
                                    }

                                }

                            }

                        }

                    }

                }

                
catch (Exception exception1)
                
{
                    
throw;
                }

                
if (encoding1 == null)
                
{
                    encoding1 
= Encoding.UTF8;
                }

            }

            
return encoding1;
        }



public static void Main()
{
    List
<string> lstFilePath = new List<string>()
    
{
        
"H:\\TestText\\ansi.txt",
        
"H:\\TestText\\unicode.txt",
        
"H:\\TestText\\utf8.txt"
    }
;
 
    
foreach (string filePath in lstFilePath)
    
{
        
using (StreamReader reader = new StreamReader(filePath, GetEncoding(filePath)))
        
{
            Console.WriteLine(
"读取文件" + filePath);
            Console.WriteLine(reader.ReadToEnd());
            Console.WriteLine(
"当前编码:" + reader.CurrentEncoding.EncodingName);
            Console.WriteLine(
"************************************************************");
        }

    }

}




posted on 2012-05-02 11:22 漂漂 阅读(933) 评论(0)  编辑 收藏 引用 所属分类: c#开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理