You can use the serializable attribute in conjunction with the 'settings' class. For a small amount of information, this is really your best bet, as it is easy to implement. For instance:
[Serializable] public class MySettings { public const string Extension = ".testInfo"; [XmlElement] public string GUID { get; set; } [XmlElement] public bool TurnedOn { get; set; } [XmlElement] public DateTime StartTime { get; set; } public void Save(string filePath) { XmlSerializer serializer = new XmlSerializer(typeof(MySettings)); TextWriter textWriter = new StreamWriter(filePath); serializer.Serialize(textWriter, this); textWriter.Close(); } public static MySettings Load(string filePath) { XmlSerializer serializer = new XmlSerializer(typeof(MySettings)); TextReader reader = new StreamReader(filePath); MySettings data = (MySettings)serializer.Deserialize(reader); reader.Close(); return data; } }
There you go. You can cut and paste it a lot directly into your code. Just add properties as needed and don't forget the [XMLElement] attribute in your interesting properties.
Another advantage of this design is that you don’t have to bother with the cumbersome Application.Settings approaches, and you can change your files manually if you need to.
source share