Click here to Skip to main content
13,353,106 members (55,921 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 22 Dec 2005

Need a SetConfig method for your Configuration Settings? What about an AppSettingsWriter?

, 20 Jan 2006
Rate this:
Please Sign up or sign in to vote.
Here are the configuration classes that allow you to modify your config file.


I've been using the System.Configuration classes in all kinds of applications and services. I find them very useful, and until recently, it was all that I needed to work with the XML application settings.


I've made a Visual Studio add-in. I intend to publish an article about that too. Actually, I got the idea on writing this article about configuration settings while I was working on the add-in. So, this add-in has a main form that you can click on in order to set some options and start the action. The first idea that came to my mind was to add the App.config file to store the predefined options and load them while starting. The next second, I realized that there’s no exe output from my solution so I cannot have an App.config file. Moreover, it would be nice if the options, when modified, could be persisted into the config file. Well, that’s the intrigue.

Explaining the code

There are two classes:

  • ConfigurationSettingsRW,
  • AppSettingsRW

and an interface:

  • IConfigurationRWSectionHandler

corresponding to the ones in the System.Configuration namespace.

'RW' stands for 'Read Write'.

IConfigurationRWSectionHandler interface

public interface IConfigurationRWSectionHandler
    object Create(XmlNode section);
    void Persist(object config, XmlNode section);

The Create method implementation should take care of loading the data from the XmlNode into an instance of the desired type (e.g. a collection of some kind) that would be returned.

The Persist method implementation should consider the first parameter of the same type as the instance returned by the Create method and should modify the XmlNode parameter accordingly.

ConfigurationSettingsRW class

You have the AppSettings property and the GetConfig method of course, but they're not static. The config file path has to be passed as a string to the constructor when creating an instance of this class => no more naming conventions for the config file. There are two public methods dealing with the appSettings config section: LoadAppSettings and SaveAppSettings, with no parameters and no return (void). You have nothing in the AppSettings property until you call LoadAppSettings. Setting or adding a value of a specified key in appSettings is made by using the indexer of AppSettings with the string parameter and calling SaveAppSettings afterwards.

Besides the GetConfig public method, you have the corresponding SetConfig public method. Both are calling a private method named GetSectionData for getting the section XmlNode and the section handler interface. I did nothing about section groups... because I never used them. Feel free to extend the code in order to suit your needs.

private void GetSectionData(string sectionName, 
      out XmlNode sectionNode, out IConfigurationRWSectionHandler handler)
    sectionNode = null;
    handler = null;
        XmlDocument configXml = new XmlDocument();
        XmlNode node = 
          configXml.SelectSingleNode(string.Format("configuration" + 
          "/configSections/section[@name='{0}']", sectionName));
        if (node != null)
            sectionNode = 
            string typeName = node.Attributes["type"].Value;
            Type type = Type.GetType(typeName);
            handler = (IConfigurationRWSectionHandler)
    catch (Exception ex)
        throw new ConfigurationException(string.Format("Error" + 
                  " while loading {0} section.", sectionName), ex);

The SetConfig method saves the data by calling the specified (by 'configuration/configSections/section/@type' value) IConfigurationRWSectionHandler.Persist method. Of course, GetConfig gets the data by calling the corresponding IConfigurationRWSectionHandler.Create method.

AppSettingsRW class

The constructor has to get the config file path as a string parameter.

There is the AppSettings public property of type NameValueCollection.

There are the LoadFromFile and SaveToFile public methods that do exactly what their names suggest. Check this out:

/// <summary>
/// Loads the <appSettings> config section data 
/// from the configuration file into the 
/// <code>appSettings</code> member of type 
/// <code>NameValueCollection</code>.
/// </summary>
public void LoadFromFile()
    if (appSettings == null)
            XmlDocument configXml = new XmlDocument();
            XmlNode appSettingsNode = 
                NameValueSectionHandler handler = 
                    new NameValueSectionHandler();

// The NameValueCollection instance returned by the 'Create' method is readonly.
// Assigning this instance to 'appSettings' member would not allow modifications.
// It seems that by passing this instance to the NameValueCollection constructor
// there is a similar instance created that is NOT readonly. Hmmm...
                appSettings = new NameValueCollection(
                    (null, null, appSettingsNode));

        catch (Exception ex)
            throw new ConfigurationException("Error while loading appSettings." +
                " Message: " + ex.Message, ex);

/// <summary>
/// Loops through the <code>appSettings</code> NameValueCollection 
/// and recreates the XML nodes of the <appSettings> config 
/// section accordingly. It saves the configuration file afterwards.
/// </summary>
public void SaveToFile()
    if (appSettings != null)
            XmlDocument configXml = new XmlDocument();
            XmlNode appSettingsNode = 
            for (int i = 0; i < appSettings.Count; i++)
                string key = appSettings.GetKey(i);
                string val = appSettings.Get(i);

                XmlNode node = configXml.CreateNode
                    (XmlNodeType.Element, "add", "");

                XmlAttribute attr = configXml.CreateAttribute("key");
                attr.Value = key;

                attr = configXml.CreateAttribute("value");
                attr.Value = val;

        catch (Exception ex)
            throw new ConfigurationException
                ("Error while saving appSettings.", ex);

And there are two other public methods: GetValue and SetValue. I'm sure you guessed what these are for. Check the source file for details.

Using the code

You can include the source file (ConfigurationSettingsRW.cs) in your project and use the classes in the same manner as you'd use the System.Configuration classes, with the few differences and additions described above. Check out the available sample solution and sources ( to see how it's done. The config file's schema must be the one you know from the .NET Framework, with the 'configSections', 'appSettings', and everything else.

As I mentioned, I intend to write an article about some add-in. That would be a good usage sample, and I guess I could insert a link to it as soon as I get it done. Until then, you have a demo project available for download.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Alexandru Stanciu
Romania Romania
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionFileUpload control for mobile application Pin
Sheris18-Feb-09 4:20
memberSheris18-Feb-09 4:20 
AnswerRe: FileUpload control for mobile application Pin
Alexandru Stanciu18-Feb-09 4:42
memberAlexandru Stanciu18-Feb-09 4:42 
GeneralRe: FileUpload control for mobile application Pin
Sheris23-Feb-09 17:12
memberSheris23-Feb-09 17:12 
GeneralDude, most of us come to see how to use your code Pin
deerchao14-Aug-07 2:51
memberdeerchao14-Aug-07 2:51 
GeneralRe: Dude, most of us come to see how to use your code Pin
Alexandru Stanciu14-Aug-07 3:18
memberAlexandru Stanciu14-Aug-07 3:18 
GeneralRe: Dude, most of us come to see how to use your code Pin
deerchao14-Aug-07 3:34
memberdeerchao14-Aug-07 3:34 
Sorry, I did lost my patience, becasue visual studio keeps updating the app.vshost.exe.config but not app.exe.config, and I don't realize this for several hours of searching code samples, debuging, modifying codes. Now I finally found it, and fixed it.
Sorry again, but I still think an article like this should describe more on how to use the code, because it should be most reader's first concern.
GeneralAn alternative Pin
Drew Noakes24-Jan-06 0:37
memberDrew Noakes24-Jan-06 0:37 
GeneralRe: An alternative Pin
Edwin Roetman24-Jan-06 23:24
memberEdwin Roetman24-Jan-06 23:24 
GeneralNormally not allowed Pin
Ramon Smits22-Dec-05 23:40
memberRamon Smits22-Dec-05 23:40 
AnswerRe: Normally not allowed Pin
Alexandru Stanciu22-Dec-05 23:56
memberAlexandru Stanciu22-Dec-05 23:56 
GeneralRe: Normally not allowed Pin
Danny Rodriguez23-Dec-05 4:13
memberDanny Rodriguez23-Dec-05 4:13 
AnswerRe: Normally not allowed Pin
Alexandru Stanciu23-Dec-05 6:16
memberAlexandru Stanciu23-Dec-05 6:16 
GeneralRe: Normally not allowed Pin
Gav_Roberts2k510-Jan-06 4:53
memberGav_Roberts2k510-Jan-06 4:53 
GeneralRe: Normally not allowed Pin
Alexandru Stanciu10-Jan-06 5:39
memberAlexandru Stanciu10-Jan-06 5:39 
GeneralRe: Normally not allowed Pin
Gav_Roberts2k510-Jan-06 5:50
memberGav_Roberts2k510-Jan-06 5:50 
GeneralRe: Normally not allowed Pin
John Whitmire11-Aug-06 10:54
memberJohn Whitmire11-Aug-06 10:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 20 Jan 2006
Article Copyright 2005 by Alexandru Stanciu
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid