65.9K
CodeProject is changing. Read more.
Home

Reading and Writing app.config or web.config Settings - Simply

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.59/5 (10 votes)

Aug 14, 2007

CPOL

2 min read

viewsIcon

67269

downloadIcon

1264

Read and write for app.config or web.config files with two simple, tested functions

Introduction

I recently needed a core component to not only read config files (the easy part) but to also write to the config file. In .NET 2.0, you can use the new Properties/Settings feature but when using that you have to manually setup the allowable definitions for each and every project.

I wanted to be able to define a common component that had some settings and that if needed, the user could be prompted to change those settings (without trying to find a config file) and it would be saved back to the config file. I wanted to use this component on multiple projects so I just wanted to copy/paste the config settings to those projects and not have to go through a UI dialog for every setting.

*Update* Apparently there is a way to do this without using the GUI, see comments. A benefit to that would be strong typing of the settings.

This would mean a single source for settings, not having to worry about registry permissions, etc. Seems easy enough but it took longer than I thought and was more difficult than I thought. I wasn't able to find anything close to what I have here: A simple two function example with unit tests (NUnit 2.4.1 required) that proved the concept and worked without a lengthy 15 thread discussion of problems. (Well I guess we'll wait and see on that one.)

Using the Code

Download the source, run the unit tests, they should all pass. The code, demo and documentation are all the same thing.

For a given config file:

<?xml version="1.0" encoding="utf-8" ?> 

<configuration> 
    <configSections> 

        <section
           name="TestReadSection" 
           type="System.Configuration.NameValueSectionHandler"
        /> 

        <section
            name="TestWriteSection"         
            type="System.Configuration.ClientSettingsSection" 
            allowExeDefinition="MachineToLocalUser" 
            requirePermission="false" 
        /> 

    </configSections> 

    <TestReadSection> 
        <add key="ApplicationName" value="Configuration Demo"/> 
    </TestReadSection> 

    <TestWriteSection> 
        <setting name="ConnectMachine" serializeAs="String"> 
            <value>localhost</value> 
        </setting> 
    </TestWriteSection> 

</configuration>

Read settings like this:

Common.GetSectionValue("TestWriteSection", "ConnectMachine");

Write settings like this:

Common.SetSectionValue("TestWriteSection", "ConnectMachine", "abc");

Points of Interest

I wasn't able to figure out how to write to normal config sections (add key= sections) so my compromise was to allow the GetSectionValue function to read the read only and the writeable section without having to specify what it was. That way, if you have a read only setting and need to make it writeable you just have to change how the config is defined and not change all the instances where the config setting is retrieved.

Enjoy!

History

  • 15-Aug-2007 - Initial version
  • 15-Aug-2007 - Updated to include note that GUI is not necessary for .NET 2.0 Properties/Settings