在我上次编写的软件中,很简单实现了再次开始时,能显示上次关闭时的状态,只要就是用数据流将关闭时的状态写入文本中,在再次启动时将信息恢复。其中代码如下:
public void btnClose_Click(object sender, EventArgs e) { state = textBox2.Text; bn[0] = btnClose.Enabled; bn[1] = btnStart.Enabled; bn[2] = btnStop.Enabled; bn[3] = btnReset.Enabled; bn[4] = timer1.Enabled; StreamWriter sw = new StreamWriter("d://a.text"); sw.WriteLine(counter); sw.WriteLine(state); for (int i = 0; i < 5; i++) { sw.WriteLine(bn[i]); } sw.Close(); //Keep records StreamWriter aw = new StreamWriter("d://c.text"); aw.WriteLine("0"); aw.Close(); //Send close information Close(); }
再次启动时,先是判断上次关闭是否有文件留下,接着若是有就将信息写入状态中,如果没有当然就不,代码如下:
if (!File.Exists(@"d://a.text")) { btnClose.Enabled = bn[0]; btnStart.Enabled = bn[1]; btnStop.Enabled = bn[2]; btnReset.Enabled = bn[3]; timer1.Enabled = bn[4]; //Initial value } else { StreamReader sr = new StreamReader("d://a.text"); counter = Convert.ToInt32(sr.ReadLine()); textBox2.Text = sr.ReadLine(); btnClose.Enabled = Convert.ToBoolean(sr.ReadLine()); btnStart.Enabled = Convert.ToBoolean(sr.ReadLine()); btnStop.Enabled = Convert.ToBoolean(sr.ReadLine()); btnReset.Enabled = Convert.ToBoolean(sr.ReadLine()); timer1.Enabled = Convert.ToBoolean(sr.ReadLine()); sr.Close(); textBox1.Text = Convert.ToString(counter); //Historical value }