Click here to Skip to main content
Click here to Skip to main content
Go to top

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

, 14 Aug 2007
Rate this:
Please Sign up or sign in to vote.
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

License

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

Share

About the Author

Paul B.

United States United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

Comments and Discussions

 
QuestionI don't have Generics? what Now? PinmemberJasonPSage16-Aug-07 9:45 
AnswerRe: I don't have Generics? what Now? PinmemberPaul B.16-Aug-07 9:50 
GeneralSaving configuration, the .NET 2.0 way... PinmemberJon Rista15-Aug-07 7:18 
GeneralRe: Saving configuration, the .NET 2.0 way... PinmemberPaul B.15-Aug-07 7:27 
GeneralAppSettingsBase Pinmemberwkempf15-Aug-07 4:46 
GeneralRe: AppSettingsBase PinmemberPaul B.15-Aug-07 5:47 
Ah, good to know. I had lots of problems with the settings/properties stopping working every now and then and not being able to get it back without starting over. I liked the simplicity of this solution once all the little gotchas with the different classes to use, removing and re-adding sections, etc. were figured out.

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 14 Aug 2007
Article Copyright 2007 by Paul B.
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid