C#读文本文件,中文字符结果均为��,求教。

matee 2009-04-24 09:20:41
代码如下:

StreamReader reader = new StreamReader(@"C:\filename.txt");
string sign = "*";
while (!reader.EndOfStream)
{
string strLine = reader.ReadLine();
string [] strs = strLine.Split(new string[] { sign },StringSplitOptions.None);
string infoOne = strs[0];
string infoTwo = strs[1];
}


文本格式为:
book*书
English*英语

...


另外请问C#中文件操作如何判断该行有何字符,如果有跳转到下一行继续读文件。
...全文
170 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
matee 2009-04-24
  • 打赏
  • 举报
回复
非常感谢。用StreamReader reader = new StreamReader(@"C:\filename.txt",Encoding.GetEncoding("gb2312")); 解决了!:)

matee 2009-04-24
  • 打赏
  • 举报
回复
非常感谢。用StreamReader reader = new StreamReader(@"C:\filename.txt",Encoding.GetEncoding("gb2312")); 解决了!:)

yangqidong 2009-04-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 Garnett_KG 的回复:]
1.txt文件是用什么编码储存的?c#加上相应的编码试试
C# code

//ex:
StreamReader reader = new StreamReader(@"C:\filename.txt",Encoding.UTF8);



[/Quote]
还不行的话就尝试

StreamReader reader = new StreamReader(@"C:\filename.txt",Encoding.GetEncoding("gb2312"));

蓝海D鱼 2009-04-24
  • 打赏
  • 举报
回复
       Encoding  encoding;                
encoding = System.Text.Encoding.GetEncoding( "GB2312 ");
// string > bytes
byte[] bytes = encoding.GetBytes(oldHtml);
encoding = System.Text.Encoding.UTF8;
// bytes > string
string newOldHtml = encoding.GetString(bytes);
//Response.Write(newOldHtml);
这样就可以把gb2312转成utf8了
Garnett_KG 2009-04-24
  • 打赏
  • 举报
回复
1.txt文件是用什么编码储存的?c#加上相应的编码试试

//ex:
StreamReader reader = new StreamReader(@"C:\filename.txt",Encoding.UTF8);

matee 2009-04-24
  • 打赏
  • 举报
回复
结果是:
infoOne : book
infoTwo :�

110,556

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧