This may not be easy 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 using this object:
public partial class Form1 : Form { MyFormState state = new MyFormState();
In the form loading mode, check if the config exists, and 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 closes .. save the configuration:
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 participants to your state class, and they will be written to the config.xml file.
source share