Click here to Skip to main content
15,879,096 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
3.59/5 (10 votes)
14 Aug 2007CPOL2 min read 65.9K   1.3K   27   6
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
<?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:

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

Write settings like this:

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


Written By
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? Pin
JasonPSage16-Aug-07 9:45
JasonPSage16-Aug-07 9:45 
AnswerRe: I don't have Generics? what Now? Pin
Paul B.16-Aug-07 9:50
Paul B.16-Aug-07 9:50 
GeneralSaving configuration, the .NET 2.0 way... Pin
Jon Rista15-Aug-07 7:18
Jon Rista15-Aug-07 7:18 
GeneralRe: Saving configuration, the .NET 2.0 way... Pin
Paul B.15-Aug-07 7:27
Paul B.15-Aug-07 7:27 
That had turned up in my search when I was first trying to find a quick way to do this.. Way too long to read through the whole thing for me for this particular problem. I think I may have gotten the client settings part from somewhere in your article but I can't remember where I found that particular part. In my case I wanted the custom section since I like to split up the different modules used for an app into a section so I'll have a section for errorhandling settings, a section for module1 used, etc. I see lots of config files with just a bunch of settings and trying to decode ServerName from DBServer and IPAddress is difficult when they are all mixed in.
GeneralAppSettingsBase Pin
wkempf15-Aug-07 4:46
wkempf15-Aug-07 4:46 
GeneralRe: AppSettingsBase Pin
Paul B.15-Aug-07 5:47
Paul B.15-Aug-07 5:47 

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.