博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用c#读取文件内容中文是乱码的解决方法:

Posted on 2016-09-13 10:02  sky410  阅读(15171)  评论(0编辑  收藏  举报

    用c#读取文件内容中文是乱码的解决方法:

            //方法1: 
            

[csharp] view plain copy
 
  1. StreamReader din = new StreamReader(@"C:\1.txt", System.Text.Encoding.GetEncoding("gb2312"));  
  2. string html = "";  
  3. while (din.Peek() > -1)  
  4. {  
  5.     html = html + din.ReadToEnd();  
  6. }  
  7. din.Close();  

 

 

            //方法2:
            

[csharp] view plain copy
 
  1. StreamReader sr1 = new StreamReader((System.IO.Stream)File.OpenRead(filename), System.Text.Encoding.Default);  
  2. html = "";  
  3. while (sr1.Peek() > -1)  
  4. {  
  5.     html = html + sr1.ReadLine();  
  6. }  
  7. sr1.Close();  

 

 

            //方法3:
           

[csharp] view plain copy
 
    1. StreamReader objReader = new StreamReader(@"C:\1.txt", System.Text.Encoding.GetEncoding("gb2312"));  
    2. string sLine = "", html = "";  
    3. while (sLine != null)  
    4. {  
    5.     sLine = objReader.ReadLine();  
    6.     if (sLine != null)  
    7.         html += sLine;  
    8. }  
    9. objReader.Close();