本人的代码如下:
public string NumPath = “Num.txt”;
private void TextBlock_TextChanged(object sender, TextChangedEventArgs e)
{
FileStream fs = new FileStream(NumPath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
Totalnum.Text = sr.ReadLine();
sr.Close();
fs.Close();
}
然后结果是一片空白,完全没显示出来
这样莫非不对吗?顺带一题不论是textbox还是textblock都不能显示数据= =
public string NumPath = “Num.txt”;
private void TextBlock_TextChanged(object sender, TextChangedEventArgs e)
{
FileStream fs = new FileStream(NumPath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
Totalnum.Text = sr.ReadLine();
sr.Close();
fs.Close();
}
然后结果是一片空白,完全没显示出来
这样莫非不对吗?顺带一题不论是textbox还是textblock都不能显示数据= =
解决方案
40
1、打开主界面时,可以在把下边代码添加到load事件里
FileStream fs = new FileStream(NumPath, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); sr.BaseStream.Seek(0, SeekOrigin.Begin); Totalnum.Text = sr.ReadLine(); sr.Close(); fs.Close();
假如文件不大最好写成
Totalnum.Text = File.ReadAllLine(NumPath)[0];
2、需要什么时候更新显示?