请问如何用FileStream打开一个*.txt文件,为什么我打开中文显示乱码?

yibuyi 2003-08-12 09:13:25
谢谢,谁帮我写个可以运行的代码?
...全文
170 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sbwgi 2004-02-12
  • 打赏
  • 举报
回复
mark
pigpag 2003-08-12
  • 打赏
  • 举报
回复
还有,你载入文本的控件字体对不对也要看清楚
xhan2000 2003-08-12
  • 打赏
  • 举报
回复
StreamReader sr=new StreamReader(path,System.Text.Encoding.Default)
kuangren 2003-08-12
  • 打赏
  • 举报
回复
string str;
string address="";
FileStream fs;

try
{
fs=new FileStream("setip.hcl",FileMode.OpenOrCreate);
}
catch(FileNotFoundException ex)
{
MessageBox.Show("读取文件出现错误!"+ex.Message,"错误",0x00);
return;
}
StreamReader sr=new StreamReader(fs);

while((str=sr.ReadLine())!=null)
{
address=address+str;
}

sr.Close();
kuangren 2003-08-12
  • 打赏
  • 举报
回复
楼上!~
ineedtostudy 2003-08-12
  • 打赏
  • 举报
回复
编码问题
看楼上的
panyee 2003-08-12
  • 打赏
  • 举报
回复
FileStream fs = new FileStream("c:\\aaa.txt", FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
string s = System.Text.Encoding.GetEncoding("gb2312").GetString(bytes);
MessageBox.Show(s);
TheAres 2003-08-12
  • 打赏
  • 举报
回复
看这个FAQ,一般是因为编码的问题。


http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp?id=26533

110,549

社区成员

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

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

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