This may/may not be easier for you.
Start by creating a class to hold your state:
public class MyFormState {
public string ButtonBackColor { get; set; }
}
Now, declare a member for your Form
with this object:
public partial class Form1 : Form {
MyFormState state = new MyFormState();
On form load, check if the config exists, then load it:
private void Form1_Load(object sender, EventArgs e) {
if (File.Exists("config.xml")) {
loadConfig();
}
button1.BackColor = System.Drawing.ColorTranslator.FromHtml(state.ButtonBackColor);
}
private void loadConfig() {
XmlSerializer ser = new XmlSerializer(typeof(MyFormState));
using (FileStream fs = File.OpenRead("config.xml")) {
state = (MyFormState)ser.Deserialize(fs);
}
}
When your form is closing.. save the config:
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
writeConfig();
}
private void writeConfig() {
using (StreamWriter sw = new StreamWriter("config.xml")) {
state.ButtonBackColor = System.Drawing.ColorTranslator.ToHtml(button1.BackColor);
XmlSerializer ser = new XmlSerializer(typeof(MyFormState));
ser.Serialize(sw, state);
}
}
Then you can add members to your state class and they will be written into the config.xml file.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…