日期:2014-05-20  浏览次数:21165 次

C#怎样读ANSI编码的文本文件里的中文
如题,我试了网上的方法,用gb2312,UTF-8,Unicode都试了,都不行,然后我将文件另存为UTF-8或Unicode以后都可以读出中文,请问怎样用程序代码解决这个问题

------解决方案--------------------
Encoding.GetEncoding("GB18030") 与 Encoding.GetEncoding("gb2312")均可

C# code
            using (FileStream fsteam = File.OpenRead(@"D:\tmp\1.txt"))
            {
                byte[] buff = new byte[fsteam.Length];
                fsteam.Read(buff, 0, buff.Length);
                string str = Encoding.GetEncoding("GB18030").GetString(buff);
                //string str = Encoding.GetEncoding("gb2312").GetString(buff);
                Console.WriteLine(str);
            }

------解决方案--------------------
StreamReader sr = new StreamReader("x:\\xx.txt",Encoding.GetEncoding("GB18030"));
string content = sr.ReadToEnd();