用c#讀取文件內容中文是亂碼的解決方法:


    用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();  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM