首页 > Windows开发 > 详细

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

时间:2016-09-13 10:09:02      阅读:285      评论:0      收藏: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();  

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

原文:http://www.cnblogs.com/skyay/p/5867406.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!