码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-09-13 10:10:20      阅读:169      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!