views:

125

answers:

2

My app is split up into a configuration tool which writes the configuration and a viewer which just reads and uses settings from the configuration.

What techniques for storing the properties would would be recommended in this scenario? Would XML for the different categories be a good idea?

The apps are developing in C#, on .NET 3.5 and using WinForms.

+1  A: 

XML would seem the ideal choice for this.

In WinForms user settings are persisted via XML so you have all the classes and helper methods you need.

ChrisF
+1  A: 

I would have a shared assembly, which contains your settings class. You can then serialize/deserialize this class to a common place on the hard drive:

[XmlRoot()]
public class Settings
{
    private static Settings instance = new Settings();

    private Settings() {}

    /// <summary>
    /// Access the Singleton instance
    /// </summary>
    [XmlElement]
    public static Settings Instance
    {
        get
        {
            return instance;
        }
    }

    /// <summary>
    /// Gets or sets the height.
    /// </summary>
    /// <value>The height.</value>
    [XmlAttribute]
    public int Height { get; set; }

    /// <summary>
    /// Main window status (Maximized or not)
    /// </summary>
    [XmlAttribute]
    public FormWindowState WindowState
    {
        get;
        set;
    }

    /// <summary>
    /// Gets or sets a value indicating whether this <see cref="Settings"/> is offline.
    /// </summary>
    /// <value><c>true</c> if offline; otherwise, <c>false</c>.</value>
    [XmlAttribute]
    public bool IsSomething
    {
        get;
        set;
    }

    /// <summary>
    /// Save setting into file
    /// </summary>
    public static void Serialize()
    {
        // Create the directory
        if (!Directory.Exists(AppTmpFolder))
        {
            Directory.CreateDirectory(AppTmpFolder);
        }

        using (TextWriter writer = new StreamWriter(SettingsFilePath))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            serializer.Serialize(writer, Settings.Instance);
        }
    }

    /// <summary>
    /// Load setting from file
    /// </summary>
    public static void Deserialize()
    {
        if (!File.Exists(SettingsFilePath))
        {
            // Can't find saved settings, using default vales
            SetDefaults();
            return;
        }
        try
        {
            using (XmlReader reader = XmlReader.Create(SettingsFilePath))
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                if (serializer.CanDeserialize(reader))
                {
                    Settings.instance = serializer.Deserialize(reader) as Settings;
                }
            }
        }
        catch (System.Exception)
        {
            // Failed to load some data, leave the settings to default
            SetDefaults();
        }
    }
}

Your xml file will then look like this:

<?xml version="1.0" encoding="utf-8"?>
<Settings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" Height="738" WindowState="Maximized" IsSomething="false" >
</Settings>
Philip Wallace
good idea, thanks. How can I serialize for example the state of a checkbox with that?
Kai
Add a boolean property IsFooChecked. When your form loads, call Settings.Instance.Deserialize(), then set the Checked property of your control to IsFooChecked. In OnFormClosing, set IsFooChecked to the Checked property of your checkbox, and call Settings.Instance.Serialize().
Philip Wallace
thanks a lot, is there also a way improving that class for making cateogries and not storing all attributes in one tag?
Kai
You would group your properties in classes (a class per category). For example, FooClass. Then, you would have a property in the Settings class called Foo, of type FooClass - with the XmlElement attribute.
Philip Wallace
New Class:public class Appearance { public bool FullscreenMode {get;set;} public bool TrayTooltipActive {get;set;} public Appearance() { } }In Settings Class:[XmlElement] public Appearance View { get; set; }Access via: Util.Settings.Instance.View.FullscreenMode = false;Util.Settings.Instance.View.TrayTooltipActive = true;
Kai