Click here to Skip to main content
13,249,087 members (43,286 online)
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 14 Aug 2007

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


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" ?> 













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

        <setting name="ConnectMachine" serializeAs="String"> 


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.



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


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


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.

You may also be interested in...

Comments and Discussions

QuestionI don't have Generics? what Now? Pin
JasonPSage16-Aug-07 10:45
memberJasonPSage16-Aug-07 10:45 
AnswerRe: I don't have Generics? what Now? Pin
Paul B.16-Aug-07 10:50
memberPaul B.16-Aug-07 10:50 
GeneralSaving configuration, the .NET 2.0 way... Pin
Jon Rista15-Aug-07 8:18
memberJon Rista15-Aug-07 8:18 
GeneralRe: Saving configuration, the .NET 2.0 way... Pin
Paul B.15-Aug-07 8:27
memberPaul B.15-Aug-07 8:27 
GeneralAppSettingsBase Pin
wkempf15-Aug-07 5:46
memberwkempf15-Aug-07 5:46 
GeneralRe: AppSettingsBase Pin
Paul B.15-Aug-07 6:47
memberPaul B.15-Aug-07 6: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    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
Web01 | 2.8.171114.1 | Last Updated 14 Aug 2007
Article Copyright 2007 by Paul B.
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid