C# 文件读取StreamReader出错 读不出数据
privatevoidForm1_Load(objectsender,EventArgse){try{FileStreamfs=newFileStream(某路径下+"z...
private void Form1_Load(object sender, EventArgs e)
{
try
{
FileStream fs = new FileStream(某路径下 + "zuigaofen.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Unicode);
int lastzuigaofen = int.Parse(sr.ReadLine());
toolStripStatusLabel2.Text = "历史最高成绩:" + lastzuigaofen.ToString();
fs.Close();
sr.Close();
}
catch (FileNotFoundException)
{
toolStripStatusLabel2.Text = "历史最高成绩:无" ;
}
}
zuigaofen.txt数据里是1200,form1_closing里就能读取出来是1200,form1load加载到状态栏里就是0 如果改为int lastzuigaofen = sr.Read();结果就是48 展开
{
try
{
FileStream fs = new FileStream(某路径下 + "zuigaofen.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs, Encoding.Unicode);
int lastzuigaofen = int.Parse(sr.ReadLine());
toolStripStatusLabel2.Text = "历史最高成绩:" + lastzuigaofen.ToString();
fs.Close();
sr.Close();
}
catch (FileNotFoundException)
{
toolStripStatusLabel2.Text = "历史最高成绩:无" ;
}
}
zuigaofen.txt数据里是1200,form1_closing里就能读取出来是1200,form1load加载到状态栏里就是0 如果改为int lastzuigaofen = sr.Read();结果就是48 展开
展开全部
没有特殊理由的话文本文件用File.ReadAllText读
更多追问追答
追问
lastzuigaofen = int.Parse(File.ReadAllText(cundang+ "zuigaofen.txt"));
toolStripStatusLabel2.Text = "历史最高成绩:" + lastzuigaofen.ToString();
输出还是历史最高成绩:0
追答
把try catch去掉再运行
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询