Udostępnij za pośrednictwem


Simple application configuration reader/writer class

app.config and web.config are very powerfull techniques for managing application settings. For more information about using configuration file please check this article in MSDN

But the manipulation of these files has 2 issues

1) There’s no class in .NET framework to update the files so you have to write your own code to access the configuration file as any xml file and update it

2) Even If you did so, the ConfigurationSettings.AppSettings property caches the values of the when it reads them for the first time so if you read a value from app.config then you changed it and re-read it again, you will get the old value.

For those two reasons, I developed a very simple assembly called simpleConfiguration which has 1 class called AppSettings and this AppSettings class has an indexer to get/set the values of the configuration settings. And this library is thread-safe.

To read from your app.config file just use these two lines

C#

simpleConfiguration.AppSettings settings=new simpleConfiguration.AppSettings();

string myValue=settings[“myValuekey”];

VB

Dim settings As New simpleConfiguration.AppSettings

Dim name As String = settings.Item("myValuekey ")

To write to app.config

C#

simpleConfiguration.AppSettings settings=new simpleConfiguration.AppSettings();

settings[“myValuekey”]=”myValue”;

VB

Dim settings As New simpleConfiguration.AppSettings

Settings.Item(“myValuekey”)=”myValue

To download the assembly go here and to download the source files here

cheers,
Mohamed

Comments

  • Anonymous
    September 14, 2005
    It's a nice class. I made a tool for this before and it was complicated.
  • Anonymous
    September 14, 2005
    Great it's so simple and very nice.
    Thanks
  • Anonymous
    September 20, 2005
    Isn't this a bad idea, since it won't work on a non-admin PC (assuming your app is installed under Program Files)?
  • Anonymous
    October 01, 2005
    Why it won't work on non-admin PC. I don't see any reason for this!!!
  • Anonymous
    May 17, 2006
    online directory main