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

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

//方法1:

[csharp]view plaincopy

  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 plaincopy

  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 plaincopy

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