[EDIT]
Hmm, reading your question again, I think I misunderstood your needs.
If you want I can remove the answer, if it is not helpful in your situation.
One way I have used a lot is to create a class containing all properties you want to save and restore, including a
Serialize
and
Deserialize
method. Then you can use a
PropertyGrid
and set the instance of your class to the SelectedObject member of the
PropertyGrid
.
Unless you have complicated structures you use as datatypes this approach is pretty straight forward and easy to get started with.
Example class
using System;
using System.ComponentModel;
using System.IO;
using System.Xml.Serialization;
namespace Test
{
[Serializable]
public class PropertyExample
{
private string fileName;
PropertyExample()
{
fileName = "";
Integer = 42;
Text = "Hello World";
}
[DefaultValue(42)]
public int Integer { get; set; }
[DefaultValue("Hello World")]
public string Text { get; set; }
public static PropertyExample Deserialize(string fileName)
{
PropertyExample props = new PropertyExample();
try
{
if (File.Exists(fileName))
{
using (Stream str = File.OpenRead(fileName))
{
XmlSerializer xml = new XmlSerializer(typeof(PropertyExample));
props = (PropertyExample)xml.Deserialize(str);
}
}
}
catch (Exception ex)
{
}
finally
{
props.fileName = fileName;
}
return props;
}
public void Serialize()
{
using (Stream str = File.OpenWrite(fileName))
{
XmlSerializer xml = new XmlSerializer(typeof(PropertyExample));
xml.Serialize(str, this);
}
}
}
}
Code for calling the class
PropertyExample props = PropertyExample.Deserialize(@"C:\Temp\PropertyExample.xml");
propertyGrid1.SelectedObject = props;
props.Serialize();