Click here to Skip to main content
15,885,782 members
Articles / Programming Languages / C# 3.0
Article

Custom C# .NET Settings Class

Rate me:
Please Sign up or sign in to vote.
2.53/5 (7 votes)
26 Jun 2008CPOL2 min read 40.9K   534   16  
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.

C#
[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

    /// <summary>
    /// Saves this settings object to desired location
    /// </summary>
    /// <param name="fileName"></param>
    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();
    }

    /// <summary>
    /// Returns a clsSettings object, loaded from a specific location
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    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:

C#
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:

C#
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:

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

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

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

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

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

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

C#
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)


Written By
Engineer
Croatia Croatia
Just going with the flow

Comments and Discussions

 
-- There are no messages in this forum --