Click here to Skip to main content
Click here to Skip to main content

Custom C# .NET Settings Class

, 26 Jun 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Having problems changing app.config and web.config? Use your own settings class. Easy to add new keys and save to disk.

Introduction

This is my first article ever, so go easy on me. This is an article about a method of saving application settings to the permanent storage I use, and it is proven to me to be useful and easy to maintain and to add new settings members.

Background

The background to this was that I had problems changing app.config and web.config settings files to save some simple settings of my Win/web/mobile applications. So, I wrote my own class to save the application settings, and it stuck to me (even though I learned to change the app.config).

Using the code

Using the code should be easy. All classes needed are contained in a file clsSettings.cs which you include in your project. The next thing you should do is change the class namespace to match your default namespace, as it makes it easier to use.

About the clsSettings class

The class has two methods, Save and Load, which save and load the clsSettings object to/from a desired path. It also has private members and public properties which are added/changed to fit your needs.

The clsSetting class has two members which are classes of their own. One is called clsApplication and the other Paths. These two, with their members, will be saved when the Save method is called.

[Serializable]
public class clsSettings
{
    #region private Fields

    private clsApplication appRelated = new clsApplication();
    private Paths paths = new Paths();

    #endregion

    #region Public properties

    public Paths Paths
    {
        get
        {
            return paths;
        }
        set
        {
            paths = value;
        }
    }

    public clsApplication AppRelated
    {
        get
        {
            return appRelated;
        }
        set
        {
            appRelated = value;
        }
    }

    #endregion

    #region Methods: Save, Load

    /// <span class="code-SummaryComment"><summary></span>
    /// Saves this settings object to desired location
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><param name="fileName"></param></span>
    public void Save(string fileName)
    {
        // Insert code to set properties and fields of the object.
        XmlSerializer mySerializer = new XmlSerializer(typeof(clsSettings));
        // To write to a file, create a StreamWriter object.
        StreamWriter myWriter = new StreamWriter(fileName);
        mySerializer.Serialize(myWriter, this);
        myWriter.Close();
    }

    /// <span class="code-SummaryComment"><summary></span>
    /// Returns a clsSettings object, loaded from a specific location
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><param name="fileName"></param></span>
    /// <span class="code-SummaryComment"><returns></returns></span>
    public clsSettings Load(string fileName)
    {
        // Constructs an instance of the XmlSerializer with the type
        // of object that is being deserialized.
        XmlSerializer mySerializer = new XmlSerializer(typeof(clsSettings));
        // To read the file, creates a FileStream.
        FileStream myFileStream = new FileStream(fileName, FileMode.Open);
        // Calls the Deserialize method and casts to the object type.
        clsSettings pos = (clsSettings)mySerializer.Deserialize(myFileStream);
        myFileStream.Close();
        return pos;
    }

    #endregion
}

You can add members to any of these classes and they will be saved/loaded as well.

The clsApplication member class in the demo looks like this:

public class clsApplication
{
    #region private fields
    private string version = "";
    private string lsStrIDLeft = "";
    private string lsStrIDRight = "";
    #endregion

    #region public properties
    public string LsStrIDLeft
    {
        get { return lsStrIDLeft; }
        set { lsStrIDLeft = value; }
    }
    public string LsStrIDRight
    {
        get { return lsStrIDRight; }
        set { lsStrIDRight = value; }
    }
    public string Version
    {
        get { return version; }
        set { version = value; }
    }
    #endregion
}

The way to add new members is that you add a private member like:

private string version = "";

and then click the right button on the version and:

clsSettings

This will generate the public property which will get/set the value of the private member.

encapsulation1.png

The way I use this clsSetting class is that I add a public static member of the Program class, so I can access the setting from any other class or form in my application. Also, I add a public static method to the Program class which returns the path where the settings will be saved/loaded, like this:

static class Program
{
    //declare and initialize the public static clsSettings object
    public static clsSettings sets = new clsSettings();
    
    /// <span class="code-SummaryComment"><summary></span>
    /// Returns the settings file path
    /// <span class="code-SummaryComment"></summary></span>
    /// <span class="code-SummaryComment"><returns></returns></span>
    public static string GetSettingPath()
    {
        return Application.StartupPath + "settings.xml";
    }
.
.
.

and access it from anywhere, for example, like this:

this.Text += " " + Program.sets.AppRelated.Version;

To load the settings, call the Load method of the clsSettings instance; for example, like this:

Program.sets = Program.sets.Load(Program.GetSettingPath());

To save the settings, call the Save method of the clsSettings instance, like this:

Program.sets.Save(Program.GetSettingPath());

Points of interest

You can use this class to save application settings, like database paths, database addresses, various IP addresses, last selected values of list boxes, grids, combos, last open forms, web service addresses, and such..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

chmod2222
Engineer
Croatia Croatia
Just going with the flow

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.141022.2 | Last Updated 27 Jun 2008
Article Copyright 2008 by chmod2222
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid